Where子句在SQL中使用日期函数后不起作用



lasttime字段是一个时间戳,我使用日期函数将其转换为日期格式mm/dd/yyyy,但是当我运行此查询时,where子句中的过滤器不起作用。

select rule, weight, date(lasttime) as last_date_fired
from prod_frm.rules_recent
where lasttime <= '10/28/2022'
limit 2000;

我推测您在这里使用MySQL,在这种情况下,您遇到的主要问题是日期文字格式不正确。使用这个版本:

SELECT rule, weight, DATE(lasttime) AS last_date_fired
FROM prod_frm.rules_recent
WHERE lasttime <= '2022-10-28'
-- ORDER BY <one or more columns>
LIMIT 2000;

还注意使用LIMIT而不使用ORDER BY是相当无意义的。一般来说,您应该指定一些排序,以获取2000条记录。