- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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)
- Related Articles
- Using User-Defined Variables in MySQL
- User-defined variables vs Local Variables in MySQL?
- System variables vs User-defined variables in MySQL?
- Show a MySQL user-defined variables values in the result table?
- MySQL ORDER BY with numeric user-defined variable?
- How can I return a record from a table nearest to a user-defined variables value in MySQL?
- Updating a MySQL table row column by appending a value from user defined variable?
- Add user defined value to a column in a MySQL query?
- In MySQL, why a client cannot use a user-defined variable defined by another client?
- MySQL increment a database field by 1?
- Perform MySQL SELECT INTO user-defined variable
- Select into a user-defined variable with MySQL
- How can we store a value in user-defined variable?
- Increment multiple Timestamp values by setting the incremented value in a user-defined variable in SQL
- What do you mean by default MySQL database for the user?

Advertisements