0

考虑下表:

+---------+---------+------------+----------+------------+
| 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 中)?

4

2 回答 2

2

就像是

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

应该做的伎俩。

于 2012-05-02T18:45:13.687 回答
1
SELECT IF(_time < '16:00:00', _date, ADDDATE(_date, 1)) AS d, COUNT(*)
FROM `table`
GROUP BY d
于 2012-05-02T18:45:51.017 回答