向MySQL表中的值添加30天?
要向表中的值添加30天,可以将ADDDATE()
函数与UPDATE命令配合使用。语法如下:
UPDATE yourTableName SET yourDateColumnName=ADDDATE(yourDateColumnName,INTERVAL 30 DAY);
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table Add30DayDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> ShippingDate date, -> PRIMARY KEY(ID) -> );
使用insert命令在表中插入一些记录。查询如下:
mysql> insert into Add30DayDemo(ShippingDate) values('2019-02-04'); mysql> insert into Add30DayDemo(ShippingDate) values('2018-11-15'); mysql> insert into Add30DayDemo(ShippingDate) values('2013-05-18'); mysql> insert into Add30DayDemo(ShippingDate) values('2017-08-25'); mysql> insert into Add30DayDemo(ShippingDate) values('2016-03-01'); mysql> insert into Add30DayDemo(ShippingDate) values('2015-09-03');
使用select语句显示表中的所有记录。查询如下:
mysql> select *from Add30DayDemo;
以下是输出:
+----+--------------+ | Id | ShippingDate | +----+--------------+ | 1 | 2019-02-04 | | 2 | 2018-11-15 | | 3 | 2013-05-18 | | 4 | 2017-08-25 | | 5 | 2016-03-01 | | 6 | 2015-09-03 | +----+--------------+ 6 rows in set (0.00 sec)
这是使用DATEADD()
以下命令在表中添加30天值的查询:
mysql> update Add30DayDemo -> SET ShippingDate=ADDDATE(ShippingDate,INTERVAL 30 DAY); Rows matched: 6 Changed: 6 Warnings: 0
现在再次检查表记录。查询如下:
mysql> select *from Add30DayDemo;
以下是带有更新日期的输出:
+----+--------------+ | Id | ShippingDate | +----+--------------+ | 1 | 2019-03-06 | | 2 | 2018-12-15 | | 3 | 2013-06-17 | | 4 | 2017-09-24 | | 5 | 2016-03-31 | | 6 | 2015-10-03 | +----+--------------+ 6 rows in set (0.00 sec)