开发者

Pick records since last particular day of week

开发者 https://www.devze.com 2023-03-24 06:14 出处:网络
I want to pick and SUM values since last wednesday until NOW() in mysql. How can I do that? Sorry for incomplete questi开发者_StackOverflow社区on, by the last Wednesday I did not mean to hard-code th

I want to pick and SUM values since last wednesday until NOW() in mysql. How can I do that?

Sorry for incomplete questi开发者_StackOverflow社区on, by the last Wednesday I did not mean to hard-code the date, rather I want my program to run that query, so it cannot hard-code--- Needs a flexible solution. Please help...


select date_sub(now(), interval dayofweek(date_sub(now(), interval 4 day)) day);

This works on any day of the week and always returns the Wednesday which has most recently passed. On a Wednesday itself, it returns the previous Wednesday. The next day, it returns yesterday


Genesis is right (He's very right, use his suggestion), but as an intellectual exercise: This is the best pure MySQL I could think of:

SELECT * FROM TABLE 
   WHERE 
     DATE_COLUMN > DATE_SUB( NOW(), INTERVAL DAYOFWEEK(NOW()) + 3 DAY);

NOW - DAYOFWEEK => this past Saturday. Weds. is three days before that.


SELECT SUM(value) FROM table WHERE date > '2011-07-20'

You should calculate your date from your programming language (fastest solution)

0

精彩评论

暂无评论...
验证码 换一张
取 消