考虑下表:
+---------+---------+------------+----------+------------+
| 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 之后的消息必须计入第二天: - msg 1 是在 16:00 之前写的2011-01-22
,所以 count_for 是(同一天)2011-01-22
- msg 2 是写的在 16:00 之后2011-01-23
,所以 count_for 是(第二天)2011-01-24
显然,我手动完成了前 5 个。我的问题是:我可以通过什么查询自动执行此操作(在 MySQL 中)?