How to select rows that have current day#39;s timestamp?(如何选择具有当天时间戳的行?)
问题描述
我试图只从数据库表中选择今天的记录.
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上的执行计划
这篇关于如何选择具有当天时间戳的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何选择具有当天时间戳的行?
基础教程推荐
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 在多列上分布任意行 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
