How to fetch current week data till today's date ?
Example - Current week start from 2017-08-01 to 2017-08-07 and today date is 2017-08-03. I want to fetch data from 2017-08-01 to 2017-08-03 using query.
This is query -
SELECT *
FROM user_data
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
答案 0 :(得分:1)
I have resolved my problem.
SELECT *
FROM current_week
WHERE YEARWEEK(`dt`, 1) = YEARWEEK(CURDATE(), 1) and dt <= curdate() order by dt
This is working fine.
答案 1 :(得分:0)
This one gets tricky in the last and first weeks of calendar years.
First you need to decide whether your week starts on Sunday or Monday. That is based on your national jurisdiction's business practices. In North America, it's generally Sunday. In Europe it is sometimes Monday.
You need an expression that computes the first moment of a calendar week based on a date.
This is such an expression for weeks starting Sunday.
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))
This is it for weeks starting Monday. (Notice the -2
.)
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -2, 7))
Now you can select stuff from your table that's in the current week.
SELECT whatever, whatever
FROM user_data
WHERE `date` >= FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))
If you want stuff from the week before the current week use this
SELECT whatever, whatever
FROM user_data
WHERE `date` >= FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7)) - INTERVAL 7 DAY
AND `date` < FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))