此查询限制自上一小时以来的选择:
... WHERE date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 HOUR))
去年就是这样做了:
... WHERE date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 YEAR))
如何编写WHERE
子句以限制 last hours 和去年之间的选择?
答案 0 :(得分:1)
where date_time between unix_timestamp(DATE_SUB(now(), INTERVAL 1 YEAR)) and unix_timestamp(DATE_SUB(now(), INTERVAL 1 HOUR))
答案 1 :(得分:1)
使用between
:
WHERE from_unixtime(date_time) BETWEEN (now() - INTERVAL 1 year) AND (now() - INTERVAL 1 hour)
评论后续:
mysql> select now() - interval 1 year, date_sub(now(), interval 1 year);
+-------------------------+----------------------------------+
| now() - interval 1 year | date_sub(now(), interval 1 year) |
+-------------------------+----------------------------------+
| 2015-09-09 09:25:59 | 2015-09-09 09:25:59 |
+-------------------------+----------------------------------+
1 row in set (0.00 sec)