gpt4 book ai didi

MySQL DATE_ADD() 带减法

转载 作者:行者123 更新时间:2023-11-29 07:41:30 26 4
gpt4 key购买 nike

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 |
+----+-----------+---------------------+---------------------+---------------------+---------------------+--------+

最佳答案

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

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
)

关于MySQL DATE_ADD() 带减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28950533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com