SQL查询多个物料的增加物料价格

问题描述 投票:2回答:4

我想编写Sql查询以按百分比增加商品价格。

场景是:-

在表中,我有3列文字:ID,商品名称,价格

Example : If item-Name is T-shirt, Increase price by 10%

         item-Name is Jins , Increase price by 50%

         item-Name is top , Increase price by 5%
mysql sql select sql-update case
4个回答
2
投票

如果要更新表,则可以进行条件更新。

update table_name
set 
price = 
case 
 when `Item-Name` = 'T-shirt' then price+( (price*10) /100 )
 when `Item-Name` = 'Jins' then price+( (price*50) /100 )
 when `Item-Name` = 'top' then price+( (price*5) /100 )
end ;

并且如果您希望在选择时不对表进行任何更新就显示增加的价格,则可以执行以下操作。

select id,`Item-Name`,price,
case 
     when `Item-Name` = 'T-shirt' then price+( (price*10) /100 )
     when `Item-Name` = 'Jins' then price+( (price*50) /100 )
     when `Item-Name` = 'top' then price+( (price*5) /100 )
     else price
    end as new_price from table_name;

1
投票

尝试一下:

SELECT a.ID, a.ItemName, a.Price, 
        (CASE WHEN a.ItemName = 'T-shirt' THEN (a.price * 10 / 100) 
              WHEN a.ItemName = 'Jins' THEN (a.price * 50 / 100) 
              WHEN a.ItemName = 'top' THEN (a.price * 5 / 100) 
              ELSE a.price 
         END) AS calculatedPrice
FROM tableA a 

0
投票
UPDATE TABLENAME SET price = (price*1.1) where item-Name = "T-shirt";

UPDATE TABLENAME SET price = (price*1.5) where item-Name = "Jins";

UPDATE TABLENAME SET price = (price*1.05) where item-Name = "Top";

我认为这可行...

© www.soinside.com 2019 - 2024. All rights reserved.