如何在 MySQL 中减去时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Subtract Time in MySQL
提问by AndreKR
How can I subtract time in MySQL? For example, today is 16 March; I want to subtract 15 days to reach 1 March. Are there any methods that can be used to subtract 15 days from the current date?
如何在 MySQL 中减去时间?例如,今天是 3 月 16 日;我想减去 15 天到 3 月 1 日。有没有什么方法可以从当前日期减去 15 天?
回答by AndreKR
SELECT DATE(NOW()-INTERVAL 15 DAY)
For a list of units see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
有关单位列表,请参阅http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
回答by Riaan Schutte
Not entirely related to this question but is related to the title.
与此问题不完全相关,但与标题相关。
SELECT SUBTIME("10:24:21", "5");
subtracts 5 seconds.
减去 5 秒。
SELECT SUBTIME("10:24:21", "01:00:00");
subtracts one hour.
减去一小时。
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime
回答by Nae
Use:
用:
SELECT NOW() - INTERVAL 15 DAY
to keep the datetime precision.
保持日期时间精度。
回答by Pancham
You can use this :
你可以使用这个:
SELECT DATE(NOW()-INTERVAL 15 DAY);
SELECT DATE(NOW()-INTERVAL 15 DAY);
for when you want to subtract the number of days.
当你想减去天数时。
In order to subtract the time instead, say 15 minutes, the following should work:
为了减去时间,比如 15 分钟,以下应该有效:
SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));
SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));
Adding the reference link again :- https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add.
再次添加参考链接:- https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add。