简体   繁体   English

在mysql之间的where子句不起作用

[英]Between in mysql where clause not working

I am trying to select data from a date range using BETWEEN in the where clause,But it does not return the correct result.The date field is in the format 'mm/dd/YYYY' and the type is varchar My query, 我试图在where子句中使用BETWEEN从日期范围中选择数据,但是它没有返回正确的结果。date字段的格式为'mm/dd/YYYY' ,类型为varchar我的查询是,

select * from time_track where track_date between '01/01/2015' and '01/21/2015'

But the result contain data with year 2014. 但是结果包含2014年的数据。

Can anyone please help me 谁能帮帮我吗

You need to convert the date string to a real date 您需要将日期字符串转换为实际日期

select * from time_track 
where STR_TO_DATE(track_date ,'%d/%m/%Y') between '2015-01-01' and '2015-01-21'

Or even better use the date data type for that column. 甚至更好地使用该列的日期数据类型。

Then you don't need to convert the data in your queries and can make use of indexes which makes it run super fast. 这样,您就无需在查询中转换数据,而可以利用索引来使其运行速度超快。

我得到了答案。

SELECT * FROM `time_track` where DATE_FORMAT(STR_TO_DATE(track_date, '%c/%d/%Y'), '%Y-%m-%d') between '2015-01-01' and '2015-01-21'

尝试使用此格式,从time_track中选择*,其中track_date> ='2015-01-01 00:00:00'AND track_date <='2015-01-01 00:00:00'

First convert two dates you have selected 首先转换您选择的两个日期

$from=strtotime(date("Y-m-d H:i:s", strtotime($yourfromdate)))
$to=strtotime(date("Y-m-d H:i:s", strtotime($yourtodate)))

select * from time_track where track_date>='$from' and track_date<='$to'

the format of your date is incorrect. 您的日期格式不正确。 You must use this format in your query: yyyy-mm-dd. 您必须在查询中使用以下格式:yyyy-mm-dd。

select * from time_track where track_date between '2015-01-01' and '2015-01-21' 从time_track中选择*,其中track_date在'2015-01-01'和'2015-01-21'之间

Marco 马可

For mysql between clause to work expectedly, ideal values would be integer, date, time or datetime. 为了使mysql between子句正常工作,理想值为整数,日期,时间或日期时间。 Try changing the datatype from varchar to datetime. 尝试将数据类型从varchar更改为datetime。 That would do your comparison job. 那将做您的比较工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM