获得产品的最低价格。
说明我的问题:
第1行
Product_Id
= 1Product_Name
=“iPhone 5”Market_Name
=“沃尔玛”Product_Original_Price
=“359.00”Product_Promotional_Price
=“319.00”Product_State
= 1(正在提供)第2行
Product_Id
= 1Product_Name
=“iPhone 5”Market_Name
=“Apple”Product_Original_Price
=“359.00”Product_Promotional_Price
=“0.00”Product_State
= 0(未提供)第3行
Product_Id
= 1Product_Name
=“iPhone 5”Market_Name
=“BestBuy”Product_Original_Price
=“359.00”Product_Promotional_Price
=“299.00”Product_State
= 1(正在提供)
下一个主题的查询( 我有什么 )让我回归零作为上述问题的最佳价格 - 但最好的价格是{{1 } {},299.00
,因为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`;
确定是否提供产品,请遵循以下片段:
Product_State
你能看到 IF / THEN / ELSE 吗?这是与之前的查询相关的内容。
上述查询不起作用 - 语法不正确,我知道,但这只是为了说明。
Gordon Linoff发布了this answer并且随之而来,我做了这个:
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`
[...]
为了澄清,我只是将他的[Gordon Linoff]语法调整到我的方案中 - 使用ROUND
来舍入数字,使用CAST
将值设置为某种类型。
工作得很好!! 谢谢!!
答案 0 :(得分:4)
您需要修复逻辑才能获得最低价格。案例陈述是最好的方式。这是一个例子:
select MIN(case when `Product_Promotional_Price` = 0 then `Product_Original_Price`
else least(`Product_Promotional_Price`, `Product_Original_Price`)
end)
答案 1 :(得分:0)
将where Product_Original_Price!=0 and Product_Promotional_Price!=0
放到最后;