mysqlsqldateadd函数_mysql中date_add()函数的使⽤?需求描述:
在使⽤mysql的过程中,需要对⽇期进⾏计算,⽐如对某个⽇期加上⼏天,⼏个⼩时等操作,
在此记录下,date_add()函数的使⽤.
操作过程:
date_add()函数语法:
备注:date_add()和adddate()是同义词.
参数说明:
date:起始⽇期或者起始时间
expr:指定的是⼀个间隔值,在起始时间中增加或者减少,注意:expr是⼀个字符串.对于负值间隔,可以以"-"开头
unit:表⽰的是⼀个单位,⽐如,加上的是1天还是⼀个⼩时.
1.对某个⽇期加上n天的操作
mysql> select date_add('2018-06-26',INTERVAL '5' day);+-----------------------------------------+
| date_add('2018-06-26',INTERVAL '5' day) |
+-----------------------------------------+
| 2018-07-01 |
+-----------------------------------------+
1 row in set (0.00sec)
mysql> select date_add('2018-06-26',INTERVAL '-5' day);+------------------------------------------+
| date_add('2018-06-26',INTERVAL '-5' day) |
+------------------------------------------+
| 2018-06-21 |
+------------------------------------------+
1 row in set (0.01 sec)
备注:expr是字符串,如果加上的正值的天数,直接'5'即可,如果是减可以⽤date_sub函数或者expr为负值'-5'.
2.对某个⽇期加上n⼩时,n分钟,n秒的操作
mysql> select date_add('2018-06-26 23:59:59',INTERVAL 1hour); #对于⽇期加上1⼩时+-------------------------------------------------+
| date_add('2018-06-26 23:59:59',INTERVAL 1 hour) |
+-------------------------------------------------+
| 2018-06-27 00:59:59 |
+-------------------------------------------------+
1 row in set (0.00sec)
mysql> select date_add('2018-06-26 23:59:59',INTERVAL 1minute); #对于⽇期加上1分钟+---------------
------------------------------------+
| date_add('2018-06-26 23:59:59',INTERVAL 1 minute) |
+---------------------------------------------------+
| 2018-06-27 00:00:59 |
+---------------------------------------------------+mysql下载add produce
1 row in set (0.00sec)
mysql> select date_add('2018-06-26 23:59:59',INTERVAL 1second); #对⽇期加上1秒钟+---------------------------------------------------+
| date_add('2018-06-26 23:59:59',INTERVAL 1 second) |
+---------------------------------------------------+
| 2018-06-27 00:00:00 |
+---------------------------------------------------+
1 row in set (0.00 sec)
3.对于某个⽇期加上n分钟n秒
mysql> select date_add('2018-06-26 23:59:59',INTERVAL '1:1'MINUTE_SECOND);+--------------------------------------------------------------+
| date_add('2018-06-26 23:59:59',INTERVAL '1:1' MINUTE_SECOND) |
+--------------------------------------------------------------+
| 2018-06-27 00:01:00 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
备注:⼀次性加上1分钟,⼀秒.
4.对于某个⽇期加上n⼩时n分钟n秒
mysql> select date_add('2018-06-26 23:59:59',INTERVAL '1:1:1'HOUR_SECOND);+--------------------------------------------------------------+
| date_add('2018-06-26 23:59:59',INTERVAL '1:1:1' HOUR_SECOND) |
+--------------------------------------------------------------+
| 2018-06-27 01:01:00 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
备注:单位HOUR_SECOND就是从⼩时到秒.expr:HOURS:MINUTES:SECONDS
5.对某个⽇期加上n⼩时n分钟
mysql> select date_add('2018-06-26 23:59:59',INTERVAL '1:1'HOUR_MINUTE);+------------------------------------------------------------+
| date_add('2018-06-26 23:59:59',INTERVAL '1:1' HOUR_MINUTE) |

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。