函数:date_add()
set @dt = now();
select date_add(@dt, interval 1 day); — add 1 day
select date_add(@dt, interval 1 hour); — add 1 hour
select date_add(@dt, interval 1 minute); — …
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day); — sub 1 day
eg:
SELECT
t.fbdate,
c.noticeStatus
FROM
lease_leasor_demandinfo t
LEFT JOIN lease_leasor_awardnotice c ON t.id = c.mid
WHERE
DATE_FORMAT( t.fbdate, '%m-%d-%Y:%h' ) BETWEEN DATE_FORMAT( date_add( now( ),interval - 5 DAY ), '%m-%d-%Y:%h' )
AND DATE_FORMAT( now( ), '%m-%d-%Y:%h' )
MySQL adddate(), addtime()函数,可以用 date_add()来替代。下面是 date_add() 实现 addtime() 功能示例:
mysql> set @dt = ‘2008-08-09 12:12:33′;
mysql> select date_add(@dt, interval ’01:15:30′ hour_second);
+————————————————+
| date_add(@dt, interval ’01:15:30′ hour_second) |
+————————————————+
| 2008-08-09 13:28:03 |
+————————————————+
mysql> select date_add(@dt, interval ‘1 01:15:30′ day_second);
+————————————————-+
| date_add(@dt, interval ‘1 01:15:30′ day_second) |
+————————————————-+
| 2008-08-10 13:28:03 |
+————————————————-+
MySQL 为日期减去一个时间间隔
函数:date_sub()
mysql> select date_sub(‘1998-01-01 00:00:00′, interval ‘1 1:1:1′ day_second);
+—————————————————————-+
| date_sub(‘1998-01-01 00:00:00′, interval ‘1 1:1:1′ day_second) |
+—————————————————————-+
| 1997-12-30 22:58:59 |
+—————————————————————-+