根据日期选择记录

时间:2016-01-28 07:31:22

标签: mysql date

我有一个表创建时间戳字段的员工表我想运行一个查询,我可以从今天日期中选择在3到5个月之间创建的员工。所以,如果

Employee A : 2016 - 01 - 28
Employee B : 2015 -  12 - 14 (less than 3 months from today)
Employee C : 2015 - 9 - 11 (more than 3 months but less than 5 months from today)
Employee D : 2015 - 01 - 20 (more than 5 months from today)

然后我的查询应该只返回Employee C.

到目前为止,我已经想出了这个。

SELECT *
FROM employees
WHERE createdAt > NOW() - INTERVAL 2 Months AND createdAt < NOW() - INTERVAL 5 Months

1 个答案:

答案 0 :(得分:1)

lower日期开始到higher。你可以试试这个:

  SELECT * FROM employees WHERE date(createdAt) between  DATE_SUB(NOW(), INTERVAL 5 MONTH) and  DATE_SUB(NOW(), INTERVAL 3 MONTH)