#### MySQL DATE_ADD() 函数
~~~
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
~~~
These functions perform date arithmetic. The date argument specifies the starting date or datetime value. expr is an expression specifying the interval value to be added or subtracted from the starting date. expr is a string; it may start with a - for negative intervals. unit is a keyword indicating the units in which the expression should be interpreted.
The INTERVAL keyword and the unit specifier are not case sensitive.
The following table shows the expected form of the expr argument for each unit value.
|unit Value |Expected expr Format|
|--|--|
|MICROSECOND| MICROSECONDS|
|SECOND |SECONDS|
|MINUTE |MINUTES|
|HOUR |HOURS|
|DAY |DAYS|
|WEEK |WEEKS|
|MONTH |MONTHS|
|QUARTER |QUARTERS|
|YEAR |YEARS|
|SECOND_MICROSECOND |'SECONDS.MICROSECONDS'|
|MINUTE_MICROSECOND| 'MINUTES:SECONDS.MICROSECONDS'|
|MINUTE_SECOND| 'MINUTES:SECONDS'|
|HOUR_MICROSECOND |'HOURS:MINUTES:SECONDS.MICROSECONDS'|
|HOUR_SECOND| 'HOURS:MINUTES:SECONDS'|
|HOUR_MINUTE |'HOURS:MINUTES'|
|DAY_MICROSECOND| 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'|
|DAY_SECOND| 'DAYS HOURS:MINUTES:SECONDS'|
|DAY_MINUTE| 'DAYS HOURS:MINUTES'|
|DAY_HOUR| 'DAYS HOURS'|
|YEAR_MONTH |'YEARS-MONTHS'|
The return value depends on the arguments:
* DATETIME if the first argument is a DATETIME (or TIMESTAMP) value, or if the first argument is a DATE and the unit value uses HOURS, MINUTES, or SECONDS.
* String otherwise.
To ensure that the result is DATETIME, you can use CAST() to convert the first argument to DATETIME.
MySQL permits any punctuation delimiter in the expr format. Those shown in the table are the suggested delimiters. If the date argument is a DATE value and your calculations involve only YEAR, MONTH, and DAY parts (that is, no time parts), the result is a DATE value. Otherwise, the result is a DATETIME value.
Date arithmetic also can be performed using INTERVAL together with the + or - operator:
~~~
date + INTERVAL expr unit
date - INTERVAL expr unit
~~~
INTERVAL expr unit is permitted on either side of the + operator if the expression on the other side is a date or datetime value. For the - operator, INTERVAL expr unit is permitted only on the right side, because it makes no sense to subtract a date or datetime value from an interval.
~~~
mysql> SELECT '2008-12-31 23:59:59' + INTERVAL 1 SECOND;
-> '2009-01-01 00:00:00'
mysql> SELECT INTERVAL 1 DAY + '2008-12-31';
-> '2009-01-01'
mysql> SELECT '2005-01-01' - INTERVAL 1 SECOND;
-> '2004-12-31 23:59:59'
mysql> SELECT DATE_ADD('2000-12-31 23:59:59',
-> INTERVAL 1 SECOND);
-> '2001-01-01 00:00:00'
mysql> SELECT DATE_ADD('2010-12-31 23:59:59',
-> INTERVAL 1 DAY);
-> '2011-01-01 23:59:59'
mysql> SELECT DATE_ADD('2100-12-31 23:59:59',
-> INTERVAL '1:1' MINUTE_SECOND);
-> '2101-01-01 00:01:00'
mysql> SELECT DATE_SUB('2005-01-01 00:00:00',
-> INTERVAL '1 1:1:1' DAY_SECOND);
-> '2004-12-30 22:58:59'
mysql> SELECT DATE_ADD('1900-01-01 00:00:00',
-> INTERVAL '-1 10' DAY_HOUR);
-> '1899-12-30 14:00:00'
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
mysql> SELECT DATE_ADD('1992-12-31 23:59:59.000002',
-> INTERVAL '1.999999' SECOND_MICROSECOND);
-> '1993-01-01 00:00:01.000001'
~~~
If you specify an interval value that is too short (does not include all the interval parts that would be expected from the unit keyword), MySQL assumes that you have left out the leftmost parts of the interval value. For example, if you specify a unit of DAY_SECOND, the value of expr is expected to have days, hours, minutes, and seconds parts. If you specify a value like '1:10', MySQL assumes that the days and hours parts are missing and the value represents minutes and seconds. In other words, '1:10' DAY_SECOND is interpreted in such a way that it is equivalent to '1:10' MINUTE_SECOND. This is analogous to the way that MySQL interprets TIME values as representing elapsed time rather than as a time of day.
Because expr is treated as a string, be careful if you specify a nonstring value with INTERVAL. For example, with an interval specifier of HOUR_MINUTE, 6/4 evaluates to 1.5000 and is treated as 1 hour, 5000 minutes:
~~~
mysql> SELECT 6/4;
-> 1.5000
mysql> SELECT DATE_ADD('2009-01-01', INTERVAL 6/4 HOUR_MINUTE);
-> '2009-01-04 12:20:00'
~~~
To ensure interpretation of the interval value as you expect, a CAST() operation may be used. To treat 6/4 as 1 hour, 5 minutes, cast it to a DECIMAL value with a single fractional digit:
~~~
mysql> SELECT CAST(6/4 AS DECIMAL(3,1));
-> 1.5
mysql> SELECT DATE_ADD('1970-01-01 12:00:00',
-> INTERVAL CAST(6/4 AS DECIMAL(3,1)) HOUR_MINUTE);
-> '1970-01-01 13:05:00'
~~~
If you add to or subtract from a date value something that contains a time part, the result is automatically converted to a datetime value:
~~~
mysql> SELECT DATE_ADD('2013-01-01', INTERVAL 1 DAY);
-> '2013-01-02'
mysql> SELECT DATE_ADD('2013-01-01', INTERVAL 1 HOUR);
-> '2013-01-01 01:00:00'
~~~
If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a day that is larger than the maximum day for the new month, the day is adjusted to the maximum days in the new month:
~~~
mysql> SELECT DATE_ADD('2009-01-30', INTERVAL 1 MONTH);
-> '2009-02-28'
~~~
Date arithmetic operations require complete dates and do not work with incomplete dates such as '2006-07-00' or badly malformed dates:
~~~
mysql> SELECT DATE_ADD('2006-07-00', INTERVAL 1 DAY);
-> NULL
mysql> SELECT '2005-03-32' + INTERVAL 1 MONTH;
-> NULL
~~~
* * * * *
- 目录
- 数据库表
- 13.7.5.6 SHOW COLUMNS Syntax
- Mysql生成列
- 表注释
- 默认字符集
- 索引
- 外键约束
- 数据类型
- NULL
- 查询语句
- IF
- CASE
- FIND_IN_SET
- 更新语句
- 复制更新
- 截取更新
- 插入语句
- 从一张表查询一个字段值插入另一个表中
- 统计函数
- COUNT
- 分组统计
- 日期时间字段按照日期分组
- GROUP_CONCAT
- 多字段关联
- LENGTH
- mb_strlen()
- SUM
- 按周,按月,按日分组统计数据
- 日期时间
- MySQL日期时间数据类型使用总结
- 计算日期差
- 插入当前日期时间
- 时间比较
- datetime
- 日期时间取时间段
- 统计
- 整型unix时间戳
- 计算两条相邻记录的时间差
- 日期时间-函数
- 常用函数
- CURDATE
- DATEDIFF
- DATE_ADD
- DATE_SUB
- EXTRACT
- NOW
- TO_DAYS
- UNIX_TIMESTAMP
- 作为时间比较
- FROM_UNIXTIME
- DATE_FORMAT
- TIMESTAMPDIFF
- 触发器
- 存储过程
- MySQL存储过程的基本函数
- 分页存储过程
- 存储过程教程
- 临时表
- 备份表
- CRM
- 客户关系表
- 通讯录
- SQL函数
- ISNULL
- IFNULL
- SUBSTR
- 模糊查询
- JSON
- Json函数
- Json数据类型
- 使用Json
- 使用navicat
- 虚拟列
- CRUD
- 索引设计
- 增加多字段索引
- 共享排他锁