比较DateTime字段的MySQL忽略时间部分中的日期 [英] Comparing dates in MySQL ignoring time portion of a DateTime field

查看:1226
本文介绍了比较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'

这篇关于比较DateTime字段的MySQL忽略时间部分中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆