days_sub
功能
从给定日期或者日期时间中减去指定的天数,获得新的 DATETIME 结果。
语法
DATETIME days_sub(DATETIME|DATE date, INT n);
参数说明
date
: DATE 或者 DATETIME 类型的表达式。
n
: 要减去的指定天数。
返回值说明
返回一个 DATETIME 类型的结果。
如果任一参数为 NULL 或者不合法,则返回 NULL。
当计算结果超出 [0000-01-01 00:00:00, 9999-12-31 00:00:00] 的范围,则返回 NULL。
示例
SELECT DAYS_SUB('2022-12-20 12:00:00', 10);
+--------------------------------------------------+
| days_sub('2022-12-20 12:00:00', INTERVAL 10 DAY) |
+--------------------------------------------------+
| 2022-12-10 12:00:00 |
+--------------------------------------------------+
SELECT DAYS_SUB('2022-12-20 12:00:00', -10);
+---------------------------------------------------+
| days_sub('2022-12-20 12:00:00', INTERVAL -10 DAY) |
+---------------------------------------------------+
| 2022-12-30 12:00:00 |
+---------------------------------------------------+
SELECT DAYS_SUB('2022-12-20 12:00:00', 738874);
+------------------------------------------------------+
| days_sub('2022-12-20 12:00:00', INTERVAL 738874 DAY) |
+------------------------------------------------------+
| 0000-01-01 12:00:00 |
+------------------------------------------------------+
SELECT DAYS_SUB('2022-12-20 12:00:00', 738875);
+------------------------------------------------------+
| days_sub('2022-12-20 12:00:00', INTERVAL 738875 DAY) |
+------------------------------------------------------+
| NULL |
+------------------------------------------------------+
SELECT DAYS_SUB('2022-12-20 12:00:00', -2913550);
+--------------------------------------------------------+
| days_sub('2022-12-20 12:00:00', INTERVAL -2913550 DAY) |
+--------------------------------------------------------+
| 9999-12-31 12:00:00 |
+--------------------------------------------------------+
SELECT DAYS_SUB('2022-12-20 12:00:00', -2913551);
+--------------------------------------------------------+
| days_sub('2022-12-20 12:00:00', INTERVAL -2913551 DAY) |
+--------------------------------------------------------+
| NULL |
+--------------------------------------------------------+
关键字
DAY,day