mysql 中的更新日期 + 一年

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

Update date + one year in mysql

mysqldate

提问by Bajlo

When I want setting numerical value +1 in mysql table, I use e.g.:

当我想在 mysql 表中设置数值 +1 时,我使用例如:

UPDATE table SET number=number+1 WHEN ...

How can I set date + one year?

如何设置日期+一年?

Thanks

谢谢

回答by Julien Hoarau

You could use DATE_ADD: (or ADDDATEwith INTERVAL)

你可以使用DATE_ADD: (或ADDDATEwith INTERVAL)

UPDATE table SET date = DATE_ADD(date, INTERVAL 1 YEAR) 

回答by Fred McIntyre

This post helped me today, but I had to experiment to do what I needed. Here is what I found.

今天这篇文章对我有所帮助,但我必须尝试做我需要的事情。这是我发现的。

Should you want to add more complex time periods, for example 1 year and 15 days, you can use

如果要添加更复杂的时间段,例如 1 年 15 天,可以使用

UPDATE tablename SET datefieldname = curdate() + INTERVAL 15 DAY + INTERVAL 1 YEAR;

I found that using DATE_ADDdoesn't allow for adding more than one interval. And there is no YEAR_DAYS interval keyword, though there are others that combine time periods. If you are adding times, use now()rather than curdate().

我发现 usingDATE_ADD不允许添加多个间隔。并且没有 YEAR_DAYS 间隔关键字,尽管还有其他组合时间段的关键字。如果要添加时间,请使用now()而不是curdate()

回答by Raffael Meier

For multiple interval types use a nested construction as in:

对于多个区间类型,使用嵌套结构,如下所示:

 UPDATE table SET date = DATE_ADD(DATE_ADD(date, INTERVAL 1 YEAR), INTERVAL 1 DAY)

For updating a given date in the column dateto 1 year + 1 day

用于将列中的给定日期更新date为 1 年 + 1 天