锋言锋语

NferZhuang的自留地

mysql统计24小时内数据

获取当前日期和时间:

 select NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2017-02-13 21:05:57 |
+---------------------+
1 row in set (0.00 sec)

参考:NOW()

获取24小时前的日期和时间:

select DATE_SUB(NOW(), INTERVAL 1 DAY);
+---------------------------------+
| DATE_SUB(NOW(), INTERVAL 1 DAY) |
+---------------------------------+
| 2017-02-12 21:08:49             |
+---------------------------------+

参考:DATE_SUB

获取24小时内的数据:

select * from table_name where timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY);

时间戳转日期:

select * from table_name where FROM_UNIXTIME(timestamp) >= DATE_SUB(NOW(), INTERVAL 1 DAY)

参考:FROM_UNIXTIME

文 / nfer
LEAVE A REPLY

loading