我有下表:
----------------------------
| id | product | price |
----------------------------
| 1 | 1 | 50 |
| 2 | 1 | 39 |
| 3 | 1 | 60 |
----------------------------
我需要的是获得按产品(1)分组的最低价格(39)并保留最低价格的ID。
我有这个查询
SELECT
id,
product,
MIN(price) AS price
FROM
variant
GROUP BY
product
结果
----------------------------
| id | product | price |
----------------------------
| 1 | 1 | 39 |
----------------------------
但我正在寻找的结果是
----------------------------
| id | product | price |
----------------------------
| 2 | 1 | 39 |
----------------------------
有可能实现这个目标吗?
我做了一个小提琴:http://sqlfiddle.com/#!9/d2ddb2
答案 0 :(得分:0)
尝试:(不是很优雅但是进一步改变的好平台)
select A.*,B.id from
(SELECT product,MIN(price) AS minprice
FROM variant GROUP BY product ) A
inner join
(SELECT * FROM variant ) B
on A.product = B.product and A.minprice = B.price
答案 1 :(得分:0)
您可以将ID除以x(例如1000000,具体取决于ID的限制)并将其添加到价格中。然后取出min并稍后拆分id。这有点偏僻但应该有效。
SELECT
product,
MIN(price+(id/10000)) AS price
FROM
variant
GROUP BY
product