MySQL 为日期添加天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7820322/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:23:50  来源:igfitidea点击:

MySQL add days to a date

mysqlsql

提问by Beginner

I have a table in MySQL. What would be the sql statement look like to add say 2 days to the current date value in the table?

我在 MySQL 中有一张表。将 2 天添加到表中的当前日期值的 sql 语句是什么样的?

UPDATE classes 
SET 
date = date + 1
where id = 161

this adds one second to the value, i don't want to update the time, i want to add two days?

这会增加一秒钟的价值,我不想更新时间,我想增加两天?

回答by Bjoern

Assuming your field is a datetype (or similar):

假设您的字段是一种date类型(或类似的):

SELECT DATE_ADD(`your_field_name`, INTERVAL 2 DAY) 
FROM `table_name`;

With the example you've provided it could look like this:

使用您提供的示例,它可能如下所示:

UPDATE classes 
SET `date` = DATE_ADD(`date` , INTERVAL 2 DAY)
WHERE `id` = 161;

This approach works with datetime, too.

这种方法也适用于datetime

回答by Gustav Barkefors

UPDATE table SET nameofdatefield = ADDDATE(nameofdatefield, 2) WHERE ...

回答by Vinit Kadkol

This query stands good for fetching the values between current date and its next 3 dates

此查询适用于获取当前日期与其接下来的 3 个日期之间的值

SELECT * FROM tableName
WHERE columName BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 3 DAY)

This will eventually add extra 3 days of buffer to the current date.

这最终会为当前日期增加额外的 3 天缓冲。

回答by Marco

For your need:

满足您的需求:

UPDATE classes 
SET `date` = DATE_ADD(`date`, INTERVAL 2 DAY)
WHERE id = 161

回答by rahularyansharma

update tablename set coldate=DATE_ADD(coldate, INTERVAL 2 DAY)

回答by Valeklosse

SELECT DATE_ADD(CURDATE(), INTERVAL 2 DAY)

回答by Kris

 DATE_ADD(FROM_DATE_HERE, INTERVAL INTERVAL_TIME_HERE DAY) 

will give the Date after adjusting the INTERVAL

将在调整 INTERVAL 后给出日期

eg.

例如。

DATE_ADD(NOW(), INTERVAL -1 DAY) for deducting 1 DAY from current Day
DATE_ADD(NOW(), INTERVAL 2 DAY)  for adding 2 Days

You can use like

你可以使用像

UPDATE classes WHERE date=(DATE_ADD(date, INTERVAL 1 DAY)) WHERE id=161

回答by Romain Bruckert

SET date = DATE_ADD( fieldname, INTERVAL 2 DAY )

回答by hovszabolcs

You can leave date_add function.

你可以离开 date_add 函数。

UPDATE `table` 
SET `yourdatefield` = `yourdatefield` + INTERVAL 2 DAY
WHERE ...