How to add +1 to existing MySQL values?


Let us see an example and create a table first.

mysql> create table Add1ToExistingValue
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command.

The query is as follows

mysql> insert into Add1ToExistingValue values(10);
Query OK, 1 row affected (0.12 sec)
mysql> insert into Add1ToExistingValue values(13);
Query OK, 1 row affected (0.15 sec)
mysql> insert into Add1ToExistingValue values(15);
Query OK, 1 row affected (0.13 sec)
mysql> insert into Add1ToExistingValue values(16);
Query OK, 1 row affected (0.14 sec)
mysql> insert into Add1ToExistingValue values(20);
Query OK, 1 row affected (0.16 sec)
mysql> insert into Add1ToExistingValue values(40);
Query OK, 1 row affected (0.15 sec)
mysql> insert into Add1ToExistingValue values(50);
Query OK, 1 row affected (0.11 sec)
mysql> insert into Add1ToExistingValue values(55);
Query OK, 1 row affected (0.17 sec)
mysql> insert into Add1ToExistingValue values(56);
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement.

The query is as follows

mysql> select *from Add1ToExistingValue;

The following is the output

+-------+
| Value |
+-------+
| 10    |
| 13    |
| 15    |
| 16    |
| 20    |
| 40    |
| 50    |
| 55    |
| 56    |
+-------+
9 rows in set (0.00 sec)

Here is the query to add +1 to existing values

mysql> update Add1ToExistingValue set Value=Value+1 where Value >=20;
Query OK, 5 rows affected (0.08 sec)
Rows matched: 5 Changed: 5 Warnings: 0

Let us check the table records from the table using select statement.

The query is as follows

mysql> select *from Add1ToExistingValue;

The following is the output

+-------+
| Value |
+-------+
| 10    |
| 13    |
| 15    |
| 16    |
| 21    |
| 41    |
| 51    |
| 56    |
| 57    |
+-------+
9 rows in set (0.00 sec)

Updated on: 30-Jul-2019

546 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements