如何选择自上个小时到去年之间的行?

时间:2016-09-09 15:18:42

标签: mysql sql datetime

此查询限制自上一小时以来的选择:

... 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 去年之间的选择?

2 个答案:

答案 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)