Select all records between two datetime format

2019-02-17 14:00发布

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';

3条回答
【Aperson】
2楼-- · 2019-02-17 14:13

Try this instead:

select * from cdr
WHERE DATE(calldate) BETWEEN '20121201' AND '20121203';
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-17 14:13

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');
查看更多
Root(大扎)
4楼-- · 2019-02-17 14:22

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.

查看更多
登录 后发表回答