Increase database field value by specified percentage using user-defined variables in MySQL


Let us first create a table −

mysql> create table DemoTable
-> (
-> Amount int
-> );
Query OK, 0 rows affected (0.99 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable values(200);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(500);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+--------+
| Amount |
+--------+
| 100    |
| 200    |
| 500    |
+--------+
3 rows in set (0.00 sec)

Following is the query to increase database field value by specified percentage −

mysql> set @rate=10;
Query OK, 0 rows affected (0.00 sec)

mysql> update DemoTable
-> set Amount=Amount*(1+@rate/100);
Query OK, 3 rows affected (0.18 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable;

Output

This will produce the following output −

+--------+
| Amount |
+--------+
| 110    |
| 220    |
| 550    |
+--------+
3 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jun-2020

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements