如何在MySQL中将数字添加到当前值(同时多次)?
您可以为此使用UPDATE命令。
语法如下
update yourTableName set yourColumnName =yourColumnName +yourIntegerValue where <yourCondition>;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table addANumberToCurrentValueDemo -> ( -> Game_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Game_Score int -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(1090); mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(204); mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(510); mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(7890); mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(8999); mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(1093859); mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(157596); mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(4857567);
现在,您可以使用select语句显示表中的所有记录。
查询如下-
mysql> select *from addANumberToCurrentValueDemo;
以下是输出
+---------+------------+ | Game_Id | Game_Score | +---------+------------+ | 1 | 1090 | | 2 | 204 | | 3 | 510 | | 4 | 7890 | | 5 | 9290 | | 6 | 1093859 | | 7 | 157596 | | 8 | 4857567 | +---------+------------+ 8 rows in set (0.05 sec)
这是在MySQL中向当前值添加数字的查询
mysql> update addANumberToCurrentValueDemo set Game_Score=Game_Score+11 where Game_Id=5; Rows matched: 1 Changed: 1 Warnings: 0
现在,再次检查表记录,以验证Game_Score列已从8999更新为9010。
查询如下-
mysql> select *from addANumberToCurrentValueDemo;
以下是输出
+---------+------------+ | Game_Id | Game_Score | +---------+------------+ | 1 | 1090 | | 2 | 204 | | 3 | 510 | | 4 | 7890 | | 5 | 9301 | | 6 | 1093859 | | 7 | 157596 | | 8 | 4857567 | +---------+------------+ 8 rows in set (0.00 sec)