用于增加多个项目的项目价值价格的 Sql 查询

2022-01-17 00:00:00 sql-update sql select mysql case

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

I want to write Sql Query for increase item price by percentage.

场景是:-

在表格中,我有 3 个列:ID、项目名称、价格

In table, I have 3 coloumn : ID, Item-Name, Price

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%

推荐答案

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

If you are looking to update the table you can do conditional update.

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 ;

如果您希望在选择时在表格中不进行任何更新而显示增加的价格,那么您可以执行以下操作.

And if you are looking to show the increased price without doing any update in the table at the time of select then you can do as below.

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;

相关文章