How to add a number to a current value in MySQL (multiple times at the same time)?


You can use UPDATE command for this.

The syntax is as follows

update yourTableName set yourColumnName =yourColumnName +yourIntegerValue where <yourCondition>;

To understand the above syntax, let us create a table. The query to create a table is as follows

mysql> create table addANumberToCurrentValueDemo
   -> (
   -> Game_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Game_Score int
   -> );
Query OK, 0 rows affected (0.67 sec)

Now you can insert some records in the table using insert command. The query is as follows −

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)

Now you can display all records from the table using select statement.

The query is as follows −

mysql> select *from addANumberToCurrentValueDemo;

The following is the output

+---------+------------+
| 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)

Here is the query to add a number to a current value in 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

Now check the table records once again to verify the column Game_Score has been updated from 8999 to 9010.

The query is as follows −

mysql> select *from addANumberToCurrentValueDemo;

The following is the output

+---------+------------+
| 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)

Updated on: 30-Jul-2019

272 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements