如果价格是促销价,请检查它是否低于原价

时间:2013-06-18 14:27:12

标签: mysql sql

目标

在列表中,我需要为每个项目获得最低价格。

问题

为了获得产品的最低价格,我正在使用它:

ROUND(CAST(MIN(`map`.`Product_Original_Price`) AS DECIMAL)/100,2) as `minProductPrice`

但是,我想检查此产品是否有Product_Promotional_Price(同一个表中的另一列)。如果是,请检查它是否低于Product_Price - 这是原始价格。

无论结果如何,如前所述返回minProductPrice

所以我问: 哪个查询将此问题转化为解决方案?

其他细节

对于细节的缺乏感到抱歉。我的申请旨在比较产品价格。因此,存在一个返回产品列表的存储过程。结果是:

Image 01

Product_Promotional_Price Lava Roupas em Pó Omo Progress 5,50 Product_Original_Price正如您所见,低于Product_Promotional_Price。当这样的事情发生时,我希望得到Product_Original_Price而不是Product_Id

更多详情

说明我的问题:

  

第1行

     
      
  • Product_Name = 1
  •   
  • Market_Name =“iPhone 5”
  •   
  • Product_Original_Price =“沃尔玛”
  •   
  • Product_Promotional_Price =“359.00”
  •   
  • Product_Id =“319.00”
  •   
     

第2行

     
      
  • Product_Name = 1
  •   
  • Market_Name =“iPhone 5”
  •   
  • Product_Original_Price =“Apple”
  •   
  • Product_Promotional_Price =“359.00”
  •   
  • Product_Id =“0.00”
  •   
     

第3行

     
      
  • Product_Name = 1
  •   
  • Market_Name =“iPhone 5”
  •   
  • Product_Original_Price =“BestBuy”
  •   
  • Product_Promotional_Price =“359.00”
  •   
  • minProductPrice =“299.00”
  •   

因此,当我调用我的程序时,iPhone 5的{​​{1}}为299.00BestBuy

现在你们都明白了吗?

2 个答案:

答案 0 :(得分:3)

尝试使用LEAST(),它返回传递给它的最低值:

SELECT Product_Id,
  MIN(LEAST(Product_Original_Price, Product_Promotional_Price)) as Best_Price
FROM Product_Price
GROUP BY Product_Id;

看看这个SQL小提琴:

http://sqlfiddle.com/#!2/f380e0/1/0

答案 1 :(得分:0)

我不知道你使用的是哪个数据库,但尝试这样的事情:

MIN(
ROUND(CAST(MIN(`map`.`Product_Price`) AS DECIMAL)/100,2) 
UNION
ROUND(CAST(MIN(`map`.`Product_Promotional_Price`) AS DECIMAL)/100,2)
) as `minProductPrice`
相关问题