如何选择具有当天时间戳的行? [英] How to select rows that have current day's timestamp?

查看:27
本文介绍了如何选择具有当天时间戳的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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上的执行计划

这篇关于如何选择具有当天时间戳的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆