To increase item value for multiple items in a single query, you can use the CASE statement in MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> ProductName varchar(20), -> ProductPrice int -> ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Product-1',700); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Product-2',1000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Product-3',3000); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+--------------+ | ProductName | ProductPrice | +-------------+--------------+ | Product-1 | 700 | | Product-2 | 1000 | | Product-3 | 3000 | +-------------+--------------+ 3 rows in set (0.00 sec)
Here is the MySQL query to increase item value price for multiple items −
mysql> update DemoTable -> set ProductPrice= -> case when ProductName='Product-1' then ProductPrice+((ProductPrice*20)/100) -> when ProductName='Product-2' then ProductPrice+((ProductPrice*40)/100) -> when ProductName='Product-3' then ProductPrice+((ProductPrice*60)/100) -> end; Query OK, 3 rows affected (0.14 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+--------------+ | ProductName | ProductPrice | +-------------+--------------+ | Product-1 | 840 | | Product-2 | 1400 | | Product-3 | 4800 | +-------------+--------------+ 3 rows in set (0.00 sec)