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)
精彩评论