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

MySQLMySQLi Database

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)
raja
Published on 12-Jul-2019 16:27:05
Advertisements