如何在 MySQL 中减去天数

发布于 2024-09-30 23:32:38 字数 81 浏览 0 评论 0原文

如何减去 MySQL 中的时间?例如,今天是 3 月 16 日;我想减去 15 天来达到 3 月 1 日。有什么方法可以从当前日期减去 15 天吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无力看清 2024-10-07 23:32:38
SELECT DATE(NOW()-INTERVAL 15 DAY)

有关单位列表,请参阅 http ://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

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

素罗衫 2024-10-07 23:32:38

与这个问题不完全相关,但与标题相关:

SELECT SUBTIME("10:24:21", "5");        -- subtracts 5 seconds. (returns "10:24:16")

SELECT SUBTIME("10:24:21", "01:00:00");  -- subtracts one hour. (returns "09:24:21")

文档:MySQL SUBTIME 函数

Not entirely related to this question but is related to the title:

SELECT SUBTIME("10:24:21", "5");        -- subtracts 5 seconds. (returns "10:24:16")

SELECT SUBTIME("10:24:21", "01:00:00");  -- subtracts one hour. (returns "09:24:21")

Documentation: MySQL SUBTIME function

故乡的云 2024-10-07 23:32:38

用途:

SELECT NOW() - INTERVAL 15 DAY

保持日期时间精度。

Use:

SELECT NOW() - INTERVAL 15 DAY

to keep the datetime precision.

躲猫猫 2024-10-07 23:32:38

可以使用:

SELECT DATE(NOW()-INTERVAL 15 DAY);

当您想要减去天数时,

。为了减去时间(例如 15 分钟),应执行以下操作:

SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));

再次添加参考链接:- < a href="https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add" rel="nofollow noreferrer">https://dev.mysql .com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add。

You can use this :

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:

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.

划一舟意中人 2024-10-07 23:32:38

是的,可以在 Mysql

    select distinct
 lastname, 
      changedat, date_add(changedat, interval -15 day) as newdate
from employee_audit;

lastname 中使用date functionchangedat是字段名称,employee_audit是表名称。

输入图片此处描述

我的日期已减去 15 天 - 请检查图片。谢谢

Yes its possible using date function in Mysql

    select distinct
 lastname, 
      changedat, date_add(changedat, interval -15 day) as newdate
from employee_audit;

lastname and changedat is field name and employee_audit is table name.

enter image description here

I have subtract 15 days from my date - check image please. thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文