MySQL 选择两种日期时间格式之间的所有记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13686274/
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
Select all records between two datetime format
提问by al_alb
I have this query, i need to select all records between two dates, the mysql table is a datetime format.
我有这个查询,我需要选择两个日期之间的所有记录,mysql 表是日期时间格式。
I tried this but it didn't work.
我试过这个,但没有用。
select * from cdr
WHERE calldate BETWEEN '2012-12-01' AND '2012-12-03';
回答by Mahmoud Gamal
Try this instead:
试试这个:
select * from cdr
WHERE DATE(calldate) BETWEEN '20121201' AND '20121203';
回答by Rohit Suthar
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');
回答by Wes
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.
只是一个简单的例子。