MySQL DATE_ADD()使用Subtract

时间:2015-03-09 19:46:19

标签: mysql sql

MySQL DATE_ADD()

我想在几天内添加一个日期字符串,并从中减去几个小时和几分钟。也许它没有意义,但这就是我的意思......

INSERT INTO 
    publication (`city`, `open`, `close`, `show`, `hide`) 
VALUES (
    'pensacola', -- CITY
    NOW(),       -- OPEN
    DATE_ADD(NOW(), INTERVAL '7 0:0:-1' DAY_SECOND),  -- CLOSE
    DATE_ADD(NOW(), INTERVAL '10 -3:0:0' DAY_SECOND), -- SHOW
    DATE_ADD(NOW(), INTERVAL '17 -3:0:-1' DAY_SECOND) -- HIDE
)

输出

+----+-----------+---------------------+---------------------+---------------------+---------------------+--------+
| id | city      | open                | close               | show                | hide                | active |
+----+-----------+---------------------+---------------------+---------------------+---------------------+--------+
|  6 | pensacola | 2015-03-09 12:00:00 | 2015-03-16 12:00:01 | 2015-03-19 15:00:00 | 2015-03-26 15:00:01 |      0 |
+----+-----------+---------------------+---------------------+---------------------+---------------------+--------+

这不是我的意图,因为我在我的INTERVAL中指定了一个

期望输出

+----+-----------+---------------------+---------------------+---------------------+---------------------+--------+
|  6 | pensacola | 2015-03-09 12:00:00 | 2015-03-16 11:59:59 | 2015-03-19 09:00:00 | 2015-03-26 08:59:59 |      0 |
+----+-----------+---------------------+---------------------+---------------------+---------------------+--------+

1 个答案:

答案 0 :(得分:2)

添加天数,然后减去时间?

INSERT INTO 
    publication (`city`, `open`, `close`, `show`, `hide`) 
VALUES (
    'pensacola', -- CITY
    NOW(),       -- OPEN
    NOW() + INTERVAL  7 DAY - INTERVAL 1 SECOND, --CLOSE
    NOW() + INTERVAL 10 DAY - INTERVAL 3 HOUR, -- SHOW
    NOW() + INTERVAL 17 DAY - INTERVAL '3:0:1' HOUR_SECOND -- HIDE
)