我想从下表中的最低价格获取id_product_attribute
:
id_product | id_product_attribute | price
210 | 131 | 44.950000
210 | 132 | 39.550000
210 | 134 | 22.820000
210 | *135 | 18.250000
211 | 137 | 36.230000
211 | *138 | 28.980000
我得到的最接近的是这个查询:
SELECT
id_product,id_product_attribute,MIN(price) as price
FROM product_attribute
WHERE price IN(
SELECT MIN(price)
FROM product_attribute
GROUP BY id_product)
GROUP BY id_product
虽然成功获得了最低值(这里询问了我的时间),但它没有返回正确的id_product_attribute
。我在子查询中尝试了一些 ORDER BY
,但这对我来说也不起作用。
提前致谢!
最佳答案
您的子查询需要选择 min(price) AND id_product,并且由于这是两个字段,因此您必须连接派生表,因为您将无法使用 in()
不再匹配:
SELECT *
FROM product_attribute AS pa
LEFT JOIN (
SELECT id_product, MIN(price) as price
FROM product_attribute
GROUP BY id_product
) AS minprices
ON pa.id_product = minprices.id_product
AND pa.price = minprices.price
关于mysql - 选择表 MySQL 中的最低值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31679056/