【问题标题】:Sql Query for increase item value price for multiple item用于增加多个项目的项目价值价格的 Sql 查询
【发布时间】:2015-02-05 20:33:46
【问题描述】:

我想编写 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%

【问题讨论】:

  • 您有问题吗?否则继续,不要在表/列标识符中使用“-”。

标签: mysql sql select sql-update case


【解决方案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;

【讨论】:

  • 所以它们都是“价格除以 100”?
【解决方案2】:
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";

我认为它有效......

【讨论】:

    【解决方案3】:

    试试这个:

    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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2021-08-19
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多