douwen8118 2012-07-15 17:06
浏览 18

MySQL - 选择当前周的最短日期,不一定是一周的第一天

Using PHP/MySQL

I'm trying to create a select statement that gets the data from the least day of the current week (I'm using it to show data on a certain player 'this week'). The week starts on Sunday. Sundays's data may not always exist therefore if the Sunday data isn't found then it would use the next earliest day found, Monday, Tuesday, etc.

My date column is named 'theDate' and the datatype is 'DATE'

The query would need to be something like:

SELECT *
FROM table_name
WHERE name = '$username'
AND [...theDate = earliest day of data found for the current week week]
LIMIT 1

It would return a single row of data.

This is a query I tried for getting the 'this week' data, It doesn't seem to work correctly on Sunday's it shows nothing:

SELECT *
FROM table_name
WHERE playerName = '$username'
AND YEARWEEK(theDate) = YEARWEEK(CURRENT_DATE)
ORDER BY theDate;

This is the query that I'm using to get 'this months' data and it works even if the first day of the months data is not found, it will use the earliest date of data found in the current month/year (this query works perfect for me):

SELECT *
FROM table_name
WHERE playerName =  '$username'
AND theDate >= CAST( DATE_FORMAT( NOW(),'%Y-%m-01') AS DATE)
ORDER BY theDate
LIMIT 1
  • 写回答

3条回答 默认 最新

  • donglu5728 2012-07-15 17:50
    关注

    Without trying this, you probably need an inner query:

    select * 
    from table_name tn
    where tn.the_date = 
    (select min(the_date) 
    from table_name 
    where WEEKOFYEAR(the_date) = WEEKOFYEAR(CURDATE())
    and YEAR(the_date) = YEAR(CURDATE()))
    

    viz, give me the row(s) in the table with a date equal to the earliest date in the table in the current week and year.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度