在單個查詢中增加單個商品的專案商品價值的 MySQL 查詢?
若要增加單個查詢中多個商品的專案商品價值,你可以使用 MySQL 中的 CASE 語句。我們首先建立一個表格——
mysql> create table DemoTable -> ( -> ProductName varchar(20), -> ProductPrice int -> ); Query OK, 0 rows affected (0.51 sec)
使用插入命令在表格中插入部分記錄——
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)
使用選擇語句顯示錶格中的所有記錄——
mysql> select *from DemoTable;
這會生成以下輸出——
+-------------+--------------+ | ProductName | ProductPrice | +-------------+--------------+ | Product-1 | 700 | | Product-2 | 1000 | | Product-3 | 3000 | +-------------+--------------+ 3 rows in set (0.00 sec)
以下是增加多個商品專案商品價值的 MySQL 查詢——
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
讓我們再次檢查一下表格記錄——
mysql> select *from DemoTable;
這會生成以下輸出——
+-------------+--------------+ | ProductName | ProductPrice | +-------------+--------------+ | Product-1 | 840 | | Product-2 | 1400 | | Product-3 | 4800 | +-------------+--------------+ 3 rows in set (0.00 sec)
廣告