- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements