2014-12-08 107 views
2

我想编写Sql查询增加项目价格的百分比。Sql查询多个项目的增加项目价格价格

的情况是: -

在表中,我有3个coloumn: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% 
+1

你有问题吗?否则继续,并且不要在表格/列标识符中使用' - '。 – Strawberry 2014-12-08 08:09:54

回答

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; 
+0

所以他们都是'时间价格除以100'? – Strawberry 2014-12-08 08:15:44

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

我认为它的工作原理...