Subtracting a number from a single MySQL column value?

MySQLMySQLi Database

For this, just update the table and subtract. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable1372 values(500);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1372 values(100);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable1372 values(900);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1372 values(1000);
Query OK, 1 row affected (0.32 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1372;

This will produce the following output −

+-------+
| Value |
+-------+
|   500 |
|   100 |
|   900 |
|  1000 |
+-------+
4 rows in set (0.00 sec)

Following is the query to subtract a number from a single MySQL column value −

mysql> update DemoTable1372 set Value=Value-50 where Value=900;
Query OK, 1 row affected (0.52 sec)
Rows matched: 1  Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1372;

This will produce the following output −

+-------+
| Value |
+-------+
|   500 |
|   100 |
|   850 |
|  1000 |
+-------+
4 rows in set (0.00 sec)
raja
Published on 11-Nov-2019 09:21:49
Advertisements