开发者

Check only date from datetime field

I have a MySql database that holds datetime in one field, in this format:

'2011-01-04 16:32:49'

How can I filter out results based on this datetime开发者_运维技巧 such that it shows results only from Today. basically, I only want to compare against the date part of the field.

Can I do something like this?

Select * from table where timestamp.date() = now().date


Use DATE:

SELECT * FROM table
WHERE DATE(timestamp) = DATE(now())

Another alternative that will be able to use the index on timestamp if you have one:

SELECT * FROM table
WHERE timestamp >= DATE(now())
AND timestamp < DATE(now()) + interval 1 day


Yes you can but the column that stores the dateTime field in your database simply needs to be referenced, you should not be calling a .date() function on it.


For mySQL, this would be:

Select * from table where DATEDIFF(NOW(),timestamp) = 0

This would give you only records in which the Date is today.


  Select * from table where DATE_FORMAT(timestamp, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d') 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜