带有 DATE_FORMAT 的 MySQL where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29245754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:24:29  来源:igfitidea点击:

MySQL where clause with DATE_FORMAT

mysqldatebetweendate-range

提问by mpsbhat

I have a MySQL table with a date column.

我有一个带有日期列的 MySQL 表。

I am trying select all the rows between some particular date range as,

我正在尝试选择某个特定日期范围之间的所有行,

select * from myTable mt 
    where DATE_FORMAT(mt.DateCol, '%y-%m-%b') between '01/03/2015' and '09/03/2015'

where 01/03/2015and 09/03/2015are from and to date range selected by user which is in dd/mm/yyyyformat and my datecolumn is in yyyy-mm-ddformat. So how can I directly select the rows using DATE_FORMATfunction. The above query gives zero result.

其中01/03/201509/03/2015由用户这是在从和到日期范围内选择dd/mm/yyyy的格式和我的datecolumn是在yyyy-mm-dd格式。那么如何使用DATE_FORMAT函数直接选择行。上面的查询给出零结果。

回答by Gordon Linoff

And why would you convert a date to a string for this? Just do:

为什么要为此将日期转换为字符串?做就是了:

select *
from myTable mt
where mt.DateCol between '2015-03-01' and '2015-03-09';

If you like, you can add date()around the constants to emphasize their types:

如果您愿意,可以date()在常量周围添加以强调它们的类型:

select *
from myTable mt
where mt.DateCol between date('2015-03-01') and date('2015-03-09');