天天看点

mysql datetime 时间比较

select * from temp where mydate > '2009-06-29 04:00:44';

Select * From temp where mydate > '2009-06-29 04:00:44' ORDER BY mydate;

Select * From temp where mydate > STR_TO_DATE('2009-06-29 04:00:44', '%Y-%m-%d %H:%i:%s');

SELECT mydate, DATE_FORMAT(mydate, '%r') FROM temp;

WHERE DATEDIFF(mydata,'2008-11-20') >=0;

The Datediff method works because it compares just the date portion, and ignores the times.

WHERE mydate <= '2008-11-25 23:59:59'

WHERE mydate < '2008-11-26 00:00:00'

SELECT * FROM event WHERE start >= "2011-07-18 00:00:00" AND start <= "2011-07-19 00:00:00";

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'

mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);

mysql> SELECT ADDDATE('1998-01-02', INTERVAL 31 DAY);

mysql> SELECT ADDDATE('1998-01-02', 31);

mysql> SELECT DATE_ADD('2014-10-8 23:59:59',INTERVAL 10 SECOND);

mysql> SELECT DATE_ADD('2014-10-8 23:59:59',INTERVAL -3600 SECOND);

mysql> SELECT '2015-12-06 07:58:06' < now() and '2015-12-06 07:58:06' > DATE_ADD(now(),INTERVAL -1000 SECOND);

类型(type值) 含义 expr表达式的形式
YEAR YY
MONTH MM
DAY DD
HOUR hh
MINUTE mm
SECOND ss
YEAR_MONTH 年和月 YY和MM之间用任意符号隔开
DAY_HOUR 日和小时 DD和hh之间用任意符号隔开
DAY_MINUTE 日和分钟 DD和mm之间用任意符号隔开
DAY_SECOND 日和秒钟 DD和ss之间用任意符号隔开
HOUR_MINUTE 时和分 hh和mm之间用任意符号隔开
HOUR_SECOND 时和秒 hh和ss之间用任意符号隔开
MINUTE_SECOND 分和秒 mm和ss之间用任意符号隔开

继续阅读