How to subtract the same amount from all values in a column with MySQL?


Let us first create a table −

mysql> create table DemoTable741 (Number int);
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable741 values(70);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable741 values(55);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable741 values(89);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable741 values(79);
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable741 values(34);
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable741;

This will produce the following output -

+--------+
| Number |
+--------+
| 70     |
| 55     |
| 89     |
| 79     |
| 34     |
+--------+
5 rows in set (0.00 sec)

Following is the query to subtract the same amount from all the values in a column −

mysql> select (Number-20) AS Result from DemoTable741;

This will produce the following output -

+--------+
| Result |
+--------+
| 50     |
| 35     |
| 69     |
| 59     |
| 14     |
+--------+
5 rows in set (0.00 sec)

Updated on: 22-Aug-2019

676 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements