7

目标

获得产品的最低价格。

问题

为了说明我的问题:

第 1 行

  • Product_Id= 1
  • Product_Name=“iPhone 5”
  • Market_Name=“沃尔玛”
  • Product_Original_Price= "359.00"
  • Product_Promotional_Price= "319.00"
  • Product_State= 1(有报价)

第 2 行

  • Product_Id= 1
  • Product_Name=“iPhone 5”
  • Market_Name=“苹果”
  • Product_Original_Price= "359.00"
  • Product_Promotional_Price= "0.00"
  • Product_State= 0(不提​​供)

第 3 行

  • Product_Id= 1
  • Product_Name=“iPhone 5”
  • Market_Name=“百思买”
  • Product_Original_Price= "359.00"
  • Product_Promotional_Price= "299.00"
  • Product_State= 1(有报价)

下一个主题(我所拥有的)的查询将零作为上述问题的最佳价格返回给我——但最佳价格是299.00, by BestBuy,因为零Product_Promotional_Price表示该产品不在销售中。

是)我有的

SELECT
  MIN(LEAST(`Product_Original_Price`, `Product_Promotional_Price`)) as `minProductPrice`
[...]

细节

我的查询:

    SELECT  `pr`.`Product_Id` as `productId`,
    `pr`.`Product_Name` as `productName`,
    ROUND(CAST(MIN(`map`.`Product_Original_Price`) AS DECIMAL)/100,2) 
      as `minProductPrice`,
    `prm`.`Product_Measure_Name` as `measureName`,
    `prm`.`Product_Measure_Shortname` as `measureShortName`,
    `pri`.`Product_Thumbnail_Image_Url` as `thumbnailUrl`,
    `pr`.`Product_Markets_Quantity` as `numberOfMarketsThatHaveThisProduct`
FROM `bm_market_products` as `map`
JOIN `bm_products` as `pr` ON `map`.`Product_Id` = `pr`.`Product_Id`
JOIN `bm_products_category_relationship` as `car` ON `pr`.`Product_Id` =
      `car`.`Product_Id`
JOIN `bm_product_categories` as `ca` ON `car`.`Category_Id` = `ca`.`Category_Id`
JOIN `bm_products_measure_relationship` as `prmr` ON `pr`.`Product_Id` = 
      `prmr`.`Product_Id`
JOIN `bm_product_measures` as `prm` ON `prmr`.`Measure_Id` =
      `prm`.`Product_Measure_Id`
JOIN `bm_products_images` as `pri` ON `pr`.`Product_Id` = `pri`.`Product_Id`
WHERE ("" IS NULL OR `map`.`Product_State` = 0)
AND ("" IS NULL OR `ca`.`Category_Id` = 14)
GROUP BY `map`.`Product_Id`;

查询返回的内容:

SQL 结果

我已经尝试过的:

考虑到Product_State确定产品是否在提供,请遵循以下片段:

SELECT  `pr`.`Product_Id` as `productId`,
    `pr`.`Product_Name` as `productName`,
    (IF(`map`.`Product_State` <> 0) THEN
      MIN(LEAST(`Product_Original_Price`, `Product_Promotional_Price`))
    ELSE (`map`.Product_Original_Price) as `minProductPrice`,
    `prm`.`Product_Measure_Name` as `measureName`,
    `prm`.`Product_Measure_Shortname` as `measureShortName`,
    `pri`.`Product_Thumbnail_Image_Url` as `thumbnailUrl`,
    `pr`.`Product_Markets_Quantity` as `numberOfMarketsThatHaveThisProduct`
[...]

你能看到IF/THEN/ELSE吗?这是与上一个查询相关的添加内容。

上面的查询不起作用——我知道语法不正确,但这只是为了说明。

解决方案

戈登林诺夫发布了这个答案,我做了这个:

SELECT  [...]
    ROUND(CAST(MIN(CASE WHEN `map`.`Product_Promotional_Price` = 0 THEN `map`.`Product_Original_Price`
            ELSE LEAST(`map`.`Product_Promotional_Price`, `map`.`Product_Original_Price`)
       end) AS DECIMAL)/100,2) as `minProductPrice`,
        [...]

澄清一下,我只是将他的 [Gordon Linoff] 语法调整到我的场景中——ROUND对数字进行舍入并将CAST值设置为某种类型。

工作完美!!谢谢!!

4

2 回答 2

4

你需要修正你的逻辑以获得最低价格。案例陈述是最好的方法。这是一个例子:

select MIN(case when `Product_Promotional_Price` = 0 then `Product_Original_Price`
                else least(`Product_Promotional_Price`, `Product_Original_Price`)
           end)
于 2013-06-18T19:12:34.700 回答
0

放到where Product_Original_Price!=0 and Product_Promotional_Price!=0最后;

于 2013-06-18T19:11:22.600 回答