我试图只从数据库表中选择今天的记录.
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 ?
使用 DATE
和 CURDATE()
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上的执行计划
这篇关于如何选择具有当天时间戳的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!