Sql Query for increase item value price for multiple item

老子叫甜甜 提交于 2020-03-02 03:24:39

问题


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

Scenario is :-

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%

回答1:


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;



回答2:


Try this:

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 



回答3:


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";

I think it works...



来源:https://stackoverflow.com/questions/27353729/sql-query-for-increase-item-value-price-for-multiple-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!