比较DateTime字段的MySQL忽略时间部分中的日期

问题描述:

我需要比较MySQL中的日期,忽略 DateTime 列中的时间部分。我尝试了以下SQL。

I need to compare dates in MySQL ignoring the time portion in a DateTime column. I have tried the following SQL.

SELECT * FROM order_table where order_date=date_format('2012-05-03', '%Y-%m-%d');

即使有一个日期 2012- 05-03 10:16:46 在MySQL表中。比较MySQL中的日期时,如何忽略 DateTime 字段中的时间部分?

It doesn't retrieve any row even though there is a date 2012-05-03 10:16:46 in MySQL table. How can the time portion in the DateTime field be ignored while comparing dates in MySQL?

您可以使用 DATE 功能:

SELECT col1, col2, ..., coln
FROM order_table
WHERE date(order_date) = '2012-05-03'

但是,如果您的表格很大,并且您在订单日期 c $ c>上的索引更高效:

But this is more efficient, if your table is large and you have an index on order date:

SELECT col1, col2, ..., coln
FROM order_table
WHERE order_date >= '2012-05-03'
AND order_date < '2012-05-04'