如何将时间戳记日期与MySQL中的date-only参数进行比较? [英] How to compare timestamp dates with date-only parameter in MySQL?
本文介绍了如何将时间戳记日期与MySQL中的date-only参数进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SQL语句中,如何将保存为TIMESTAMP的日期与YYYY-MM-DD格式的日期进行比较?
In a SQL statement, how do I compare a date saved as TIMESTAMP with a date in YYYY-MM-DD format?
Ex .: SELECT * FROM table WHERE timestamp ='2012-05-05'
我希望此查询返回指定时间戳的所有行天,但它只返回具有午夜时间戳的行。
I want this query returns all rows having timestamp in the specified day, but it returns only rows having midnight timestamp.
谢谢
推荐答案
p>您可以使用 DATE()
函数来提取时间戳记的日期部分:
You can use the DATE()
function to extract the date portion of the timestamp:
SELECT * FROM table
WHERE DATE(timestamp) = '2012-05-05'
尽管如此,如果您在时间戳列上有索引,则可能会更快,因为它可以使用索引:
Though, if you have an index on the timestamp column, this would be faster because it could utilize the index:
SELECT * FROM table
WHERE timestamp BETWEEN '2012-05-05 00:00:00' AND '2012-05-05 23:59:59'
这篇关于如何将时间戳记日期与MySQL中的date-only参数进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文