我有这个表结构
我希望按价格排名前一个记录,但是对于request_id的每个唯一值,例如,如果我
request_id user_id price
1, 1, 100
1, 2, 200
1, 3, 300
2, 2, 10
2, 1, 20
2, 3, 30
然后期望的结果是:
request_id user_id price
1, 1, 100
2, 2, 10
SELECT user_id,price FROM request ORDER BY price LIMIT 1
答案 0 :(得分:1)
SELECT x.*
FROM my_table x
JOIN
( SELECT request_id,MIN(price) min_price FROM my_table GROUP BY request_id )y
ON y.request_id = x.request_id
AND y.min_price = x.price;