小编典典

SQL查询多个项目的增加项目价值价格

sql

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

场景是:-

在表中,我有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%

阅读 173

收藏
2021-03-08

共1个答案

小编典典

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

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;
2021-03-08