Remove 20% from stored price in MySQL field, using SQL query?


Let’s say the stored price is inclusive of 20% sales tax. Now, let us first create a table −

mysql> create table DemoTable
(
   Price int
);
Query OK, 0 rows affected (1.09 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(20);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(40);
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable values(80);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(60);
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Price |
+-------+
|    20 |
|    40 |
|    80 |
|    60 |
+-------+
4 rows in set (0.00 sec)

Following is the query to remove 20% from the stored price. Therefore, 120/100= 1.2 since we have 20% sales tax as well −

mysql> update DemoTable set Price=Price/1.2;
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Price |
+-------+
|    17 |
|    33 |
|    67 |
|    50 |
+-------+
4 rows in set (0.00 sec)

Updated on: 25-Sep-2019

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements