如何从两列获取MIN值以及相应的id?

时间:2018-04-20 16:27:33

标签: mysql min

我的查询确实从两列(price_base,price_special)返回最低价格,但它没有返回与找到的最低价格对应的正确store_id。

我的查询:

SELECT grocery_item.id, grocery_item.category,
grocery_category.name AS cat, grocery_item.name AS itemName,
MIN( if( grocery_price.price_special>0,
grocery_price.price_base)) AS price,
grocery_price.store_id,
grocery_store.name AS storeName
FROM grocery_item
LEFT JOIN grocery_category ON
grocery_category.id=grocery_item.category
LEFT JOIN grocery_price
ON grocery_price.item_id = grocery_item.id
LEFT JOIN grocery_store 
ON grocery_store.id=grocery_price.store_id   
WHERE grocery_price.selection='no'
AND buy='yes'
GROUP BY grocery_price.item_id
ORDER BY store_id, grocery_item.category, grocery_item.name

返回:

ID  category    cat     itemName       price    store_id    storeName
92  3   Bread/Bakery    Arnold Bread    2.14    1           Food Lion   

但是grocery_price表保存了这个信息:

item_id price_base  price_special   store_id
92      4.29        2.14            9
92      3.99        0.00            1

所以我需要返回的store_id是9(返回的storeName不会是Food Lion)

编辑:根据Uueerdo的评论工作查询(谢谢!)

SELECT minP.item_id, gi.category, gc.name AS cat,
gi.name as itemName,  gp.store_id, 
gs.name AS storeName, minP.price
FROM
(SELECT p.item_id, MIN(IF(p.price_special >0,
p.price_special,p.price_base)) AS price
FROM grocery_item AS i
INNER JOIN grocery_price AS p ON (i.id = p.item_id)
WHERE i.buy = 'yes'
GROUP BY p.item_id) AS minP
INNER JOIN grocery_item AS gi ON minP.item_id = gi.id
INNER JOIN grocery_category AS gc on gi.category = gc.id 
LEFT JOIN grocery_price AS gp
ON minP.price = IF(gp.price_special > 0,
gp.price_special,gp.price_base)
AND gp.item_id = gi.id
INNER JOIN grocery_store AS gs ON gp.store_id = gs.id
GROUP BY gi.id
ORDER BY gs.id, gi.category,gi.name

1 个答案:

答案 0 :(得分:0)

对于未分组的非聚合字段返回的值是从分组字段遇到的值(有效)随机选择'值。大多数RDBMS甚至不认为这样的查询有效,甚至更新版本的MySQL默认不允许这样的查询。

在像您这样的情况下,您需要与聚合结果相关联的非分组值(在这种情况下为min);必须将聚合查询转换为子查询,该子查询可以连接回聚合表以查找与聚合值对应的源行。

编辑:基本上,您需要稍微区别地查看问题。您目前正在查找商品的最低价格,并列出该商品的商店;您需要找到商品的最低价格,并使用它来查找具有该价格商品的商店

这可以让您获得标有"购买"

的商品的最低价格
SELECT p.item_id, MIN(IF(p.price_special > 0,p.price_special,p.price_base)) AS price
FROM grocery_item AS i
INNER JOIN grocery_price AS p ON (i.id = p.item_id)
WHERE i.buy = 'yes'
GROUP BY p.item_id

然后您可以使用它来获得其余结果:

SELECT minP.item_id
   , gi.name
   , gi.category, gc.category_name AS cat, gi.Name as itemName, gi.buy
   , gp.store_id, gp.name AS storeName
   , minP.price
FROM ([the query above]) AS minP
INNER JOIN grocery_item AS gi ON minP.item_id = gi.id
INNER JOIN grocery_category AS gc on gi.category = gc.grocery_category_id 
    /* Guessing on this join since grocery_category_id 
       was not qualified with it's table name */
INNER JOIN grocery_price AS gp 
   ON minP.price = IF(gp.price_special > 0,gp.price_special,gp.price_base)
   /* Alternatively: ON minP.price IN (gp.price_special, gp.price_base) 
      ... though this could cause false positives if the minP.price is 0 
          from one store's base price being "free"
   */
INNER JOIN grocery_store AS gs ON gp.store_id = gs.id
;