如何选择具有当天时间戳的行?

2021-11-20 00:00:00 timestamp sql mysql

我试图只从数据库表中选择今天的记录.

I am trying to select only today's records from a database table.

目前我使用

SELECT * FROM `table` WHERE (`timestamp` > DATE_SUB(now(), INTERVAL 1 DAY));

但这需要过去 24 小时的结果,我需要它只选择今天的结果,而忽略时间.如何仅根据日期选择结果?

But this takes results for the last 24 hours, and I need it to only select results from today, ignoring the time. How can I select results based on the date only ?

推荐答案

使用 DATECURDATE()

SELECT * FROM `table` WHERE DATE(`timestamp`) = CURDATE()

警告!此查询没有有效地使用索引.有关更有效的解决方案,请参阅下面的答案

Warning! This query doesn't use an index efficiently. For the more efficient solution see the answer below

查看DEMO上的执行计划

相关文章