您可以使用UPDATE命令将值添加到MySQL中的每一行。
让我们看看您的列何时是整数。语法如下:
UPDATE yourTableName SET yourIntegerColumnName = yourIntegerColumnName+anyValue; UPDATE yourTableName SET yourIntegerColumnName = anyValue WHERE yourIntegerColumnName IS NULL;
您可以为日期列名称添加一个值。语法如下:
UPDATE yourTableName SET yourDateColumnName = DATEADD(yourDateColumnName,INTERVAL anyIntegerMonth)
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table addEachRowValue -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Amount int, -> ShippingDate date, -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下:
mysql> insert into addEachRowValue(Amount,ShippingDate) values(100,'2019-02-21'); mysql> insert into addEachRowValue(Amount,ShippingDate) values(10,'2013-04-22'); mysql> insert into addEachRowValue(Amount,ShippingDate) values(110,'2015-10-25'); mysql> insert into addEachRowValue(Amount,ShippingDate) values(150,'2016-03-27'); mysql> insert into addEachRowValue(Amount,ShippingDate) values(190,'2018-12-29');
使用select语句显示表中的所有记录。查询如下:
mysql> select *from addEachRowValue;
以下是输出:
+----+--------+--------------+ | Id | Amount | ShippingDate | +----+--------+--------------+ | 1 | 100 | 2019-02-21 | | 2 | 10 | 2013-04-22 | | 3 | 110 | 2015-10-25 | | 4 | 150 | 2016-03-27 | | 5 | 190 | 2018-12-29 | +----+--------+--------------+ 5 rows in set (0.00 sec)
这是向Amount列的每一行添加一个值的查询,该值是整数类型:
mysql> update addEachRowValue -> set Amount=Amount+20; Rows matched: 5 Changed: 5 Warnings: 0
现在,您可以检查“金额”列的所有更新值。查询如下:
mysql> select Amount from addEachRowValue;
以下是输出:
+--------+ | Amount | +--------+ | 120 | | 30 | | 130 | | 170 | | 210 | +--------+ 5 rows in set (0.00 sec)
这是为日期列添加值的查询:
mysql> update addEachRowValue -> set ShippingDate=adddate(ShippingDate,interval 1 month); Rows matched: 5 Changed: 5 Warnings: 0
现在,您可以从表中检查日期列的所有更新值。查询如下:
mysql> select ShippingDate from addEachRowValue;
以下是输出:
+--------------+ | ShippingDate | +--------------+ | 2019-03-21 | | 2013-05-22 | | 2015-11-25 | | 2016-04-27 | | 2019-01-29 | +--------------+ 5 rows in set (0.00 sec)