如何从日期时间列中选择日期?

2021-11-20 00:00:00 datetime date mysql

我有一列日期时间"类型,其值类似于 2009-10-20 10:00:00

I have a column of type "datetime" with values like 2009-10-20 10:00:00

我想从日期时间中提取日期并编写如下查询:

I would like to extract date from datetime and write a query like:

SELECT * FROM 
data 
WHERE datetime = '2009-10-20' 
ORDER BY datetime DESC

以下是最好的方法吗?

SELECT * FROM 
data 
WHERE datetime BETWEEN('2009-10-20 00:00:00' AND '2009-10-20 23:59:59')
ORDER BY datetime DESC

然而这会返回一个空的结果集.有什么建议吗?

This however returns an empty resultset. Any suggestions?

推荐答案

你可以使用 MySQL 的 DATE() 函数:

You can use MySQL's DATE() function:

WHERE DATE(datetime) = '2009-10-20'

你也可以试试这个:

WHERE datetime LIKE '2009-10-20%'

有关使用 LIKE 的性能影响的信息,请参阅此答案.

See this answer for info on the performance implications of using LIKE.

相关文章