You can increase and decrease row value by 1 in MySQL using UPDATE command. The syntax is as follows −
UPDATE yourTableName set yourColumnName = yourColumnName-1 where condition;
Let us create a table to decrease row value by 1. The following is the query −
mysql> create table IncrementAndDecrementValue −> ( −> UserId int, −> UserScores int −> ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into IncrementAndDecrementValue values(101,20000); Query OK, 1 row affected (0.13 sec) mysql> insert into IncrementAndDecrementValue values(102,30000); Query OK, 1 row affected (0.20 sec) mysql> insert into IncrementAndDecrementValue values(103,40000); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from IncrementAndDecrementValue;
The following is the output;
+--------+------------+ | UserId | UserScores | +--------+------------+ | 101 | 20000 | | 102 | 30000 | | 103 | 40000 | +--------+------------+ 3 rows in set (0.00 sec)
Here is the query to decrease the UserScores value by 1 with where condition.
mysql> update IncrementAndDecrementValue set UserScores = UserScores-1 where UserId = 103; Query OK, 1 row affected (0.41 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check whether the value is updated or not. The query is as follows −
mysql> select *from IncrementAndDecrementValue;
The following is the output displaying that we have successfully decremented a row value −
+--------+------------+ | UserId | UserScores | +--------+------------+ | 101 | 20000 | | 102 | 30000 | | 103 | 39999 | +--------+------------+ 3 rows in set (0.00 sec)