如何在 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 -> ); Query OK, 0 rows affected (0.67 sec)
现在你可以使用 insert 命令在表格中插入一些记录。查询如下 −
mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(1090); Query OK, 1 row affected (0.30 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(204); Query OK, 1 row affected (0.12 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(510); Query OK, 1 row affected (0.14 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(7890); Query OK, 1 row affected (0.14 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(8999); Query OK, 1 row affected (0.11 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(1093859); Query OK, 1 row affected (0.11 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(157596); Query OK, 1 row affected (0.11 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(4857567); Query OK, 1 row affected (0.28 sec)
现在你可以使用 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; Query OK, 1 row affected (0.15 sec) 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)
商业广告