我的数据库结构包含列:id, name, value, dealer。我想检索与最低行value的 每一个 dealer。我一直在尝试弄乱MIN()and GROUP BY,仍然-没有解决方案。
id, name, value, dealer
value
dealer
MIN()
GROUP BY
解决方案1:
SELECT t1.* FROM your_table t1 JOIN ( SELECT MIN(value) AS min_value, dealer FROM your_table GROUP BY dealer ) AS t2 ON t1.dealer = t2.dealer AND t1.value = t2.min_value
解决方案2:
SELECT t1.* FROM your_table t1 LEFT JOIN your_table t2 ON t1.dealer = t2.dealer AND t1.value > t2.value WHERE t2.value IS NULL
这个问题非常有名,因此在Mysql的手册中有专门的页面。
请检查以下内容: 行包含某列的分组最大/最小值