Select all records between two datetime format

僤鯓⒐⒋嵵緔 提交于 2019-12-01 02:52:47

问题


I have this query, i need to select all records between two dates, the mysql table is a datetime format.

I tried this but it didn't work.

select * from cdr
 WHERE calldate BETWEEN '2012-12-01' AND '2012-12-03';

回答1:


Try this instead:

select * from cdr
WHERE DATE(calldate) BETWEEN '20121201' AND '20121203';



回答2:


Try this query -

SELECT * FROM cdr WHERE calldate BETWEEN str_to_date('2012-12-01','%Y-%m-%d') AND str_to_date('2012-12-03','%Y-%m-%d');



回答3:


The above will work great. If you wanted to add extra conditions, normal Boolean logic works with date/time as well so you could do something like

...
where DATE(calldate) < '20121201' AND DATE(calldate) >= '20121203' OR DATE(calldate) = '20121205'

Just a simple example.



来源:https://stackoverflow.com/questions/13686274/select-all-records-between-two-datetime-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!