Mysql 日期时间比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6787404/
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
Mysql DateTime compare
提问by pakito
Hi I have a table of the following 2 records:
嗨,我有一张包含以下 2 条记录的表格:
descript | start | end
test 1 | 2011-07-18 14:30:00 | 2011-07-18 17:00:00
test 2 | 2011-07-18 00:00:00 | 2011-07-19 00:00:00
When I tried to do a select, I can't seems to retrieve the 2nd result (test 2) which seems like clearly it is dated 19th of July.
当我尝试进行选择时,我似乎无法检索第二个结果(测试 2),这似乎很明显是 7 月 19 日。
SELECT * FROM event WHERE start >= "2011-07-18 00:00:00" AND end <= "2011-07-18 23:59:59";
Would appreciate any advise.
将不胜感激任何建议。
回答by Nemoden
"2011-07-19 00:00:00" is MOREthan "2011-07-18 23:59:59"
“2011-07-19 00:00:00”是更多比“2011-07-18二十三时59分59秒”
By your condition it should be less, so your query does not match test 2
.
根据您的条件,它应该更少,因此您的查询不匹配test 2
。
Your SQL
query should be:
您的SQL
查询应该是:
SELECT * FROM event
WHERE start >= "2011-07-18 00:00:00"
AND end <= "2011-07-19 00:00:00";
回答by ajreal
you just need to exclude column end
, like
你只需要排除列end
,比如
SELECT * FROM event
WHERE start>="2011-07-18 00:00:00" AND start<="2011-07-18 23:59:59";
回答by bash-
You can just do this :)
你可以这样做:)
SELECT * FROM event
WHERE start BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
AND end BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
This results in the times in between the range you specified for start AND end
这会导致您为开始和结束指定的范围之间的时间