#### 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
- 索引設計
- 增加多字段索引
- 共享排他鎖