简体   繁体   English

SQL查询从同一天获取记录

[英]SQL query to get records from same day

I wish to find all records of the current day. 我希望找到当天的所有记录。 I have a field Date of type DATE. 我有一个DATE类型的字段Date

If I use 如果我使用

WHERE `Date` = '2011-04-07'

it works 有用

but if I use: 但如果我使用:

WHERE  `Date`='CURDATE()'

or 要么

WHERE  `Date`='NOW()'

it does not return any results (when there actually are some). 它不会返回任何结果(实际上有一些结果)。

How do I get the current date in the right format to use it in my SQL query? 如何以正确的格式获取当前日期以在SQL查询中使用它?

I am using MySQL And the date was originally entered in the database using NOW(). 我正在使用MySQL并且最初使用NOW()在数据库中输入日期。

Use 采用

WHERE `Date`=CURDATE()

The quotes (') are used to wrap up a string (text). 引号(')用于包装字符串(文本)。

Edit: I can see now that you say the value was stored with NOW() : it probably includes a time element too. 编辑:我现在可以看到你说这个值是用NOW()存储的:它也可能包含一个时间元素。 Will update answer imminently.. 将立即更新答案..

This will compare the date part of the Date field to today's date: 这会将Date字段的日期部分与今天的日期进行比较:

WHERE DATE(`Date`)=CURDATE()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM