如何在mysql中获得min group row?

时间:2012-09-20 15:26:06

标签: mysql sql group-by

例1:

表test1的一部分:

name|mygroup| scale
----------------
ben | 1   | 1
tom | 1   | 2
jim | 2   | 1
tim | 2   | 2
..

输出应为:

name|mygroup| scale
----------------
ben | 1   | 1
jim | 2   | 1

我试试:

select name,mygroup,min(scale) from test1 group by mygroup;

但它没有给出正确的输出。

由于

1 个答案:

答案 0 :(得分:4)

SELECT t.name, t.mygroup, t.scale
    FROM test1 t
        INNER JOIN (SELECT mygroup, MIN(scale) AS MinScale
                        FROM test1
                        GROUP BY mygroup) q
            ON t.mygroup = q.mygroup
                AND t.scale = q.MinScale