请考虑下表:

+---------+---------+------------+----------+------------+
| msg_id  | user_id | _date      | _time    | count_for  |
+-------------------+------------+----------+------------+
|   1     | 1       | 2011-01-22 | 06:23:11 | 2011-01-22 |
|   2     | 1       | 2011-01-23 | 16:17:03 | 2011-01-24 |
|   3     | 1       | 2011-01-23 | 17:05:45 | 2011-01-24 |
|   4     | 2       | 2011-01-24 | 06:58:13 | 2011-01-24 |
|   5     | 2       | 2011-01-24 | 15:59:32 | 2011-01-24 |
|   6     | 3       | 2011-01-25 | 13:45:00 | ...        |
|   7     | 3       | 2011-01-26 | 13:22:34 | ...        |
+---------+---------+------------+----------+------------+


我想计算每天的邮件数量,但是今天16:00之后的邮件必须计算到第二天:
 -消息1在16:00之前写在2011-01-22上,因此count_for是(同一天)2011-01-22
 -消息2是在16:00之后写在2011-01-23上的,所以count_for是(第二天)2011-01-24

显然,我手动做了前5个。我的问题是:我可以使用哪种查询自动执行此操作(在MySQL中)?

最佳答案

就像是

SELECT count(msg_id), if(_time >= '16:00:00', _date + INTERVAL 1 DAY, _date) AS effective_date
FROM yourtable
GROUP BY effective_date


应该可以。

关于mysql - 计算(第二天)16:00之后发布的消息的日期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10419777/

10-15 06:10