mysql 判断是同一天,SQL查询从同一天获取记录

在MySQL中,当尝试使用`CURDATE()`或`NOW()`从`Date`字段获取当前日期记录时遇到问题。解决方案是使用`WHERE DATE(Date) = CURDATE()`,因为`NOW()`可能包含时间部分,而`CURDATE()`只比较日期部分,确保与`Date`字段匹配。
摘要由CSDN通过智能技术生成

I wish to find all records of the current day.

I have a field Date of type 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?

I am using MySQL

And the date was originally entered in the database using 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. Will update answer imminently..

This will compare the date part of the Date field to today's date:

WHERE DATE(`Date`)=CURDATE()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值