3

我正在尝试以下但它失败了......我想要一切,但只有在不同的名称:

所以,如果我有

NAME    A  B  C  D
-------------------
john    5  6  6  7
jack    5  3  2  4
paul    5  7  9  2
john    4  2  3  4

我只想要前三行

select distinct name, * from items order by NAME, D

这可能吗?

4

5 回答 5

2

如果您不关心在重复名称的情况下选择哪些值:

select name,
       min(a),
       min(b),
       min(c),
       min(d)
from items
group by name
order by name;

请注意,在此解决方案中,a、b、c、d 的值不一定来自同一行!

于 2012-09-25T07:08:43.713 回答
2

样本输入:

Name    A   B   C   D
john    5   6   6   7
jack    5   3   2   4
paul    5   7   9   2
john    4   2   3   4
john    5   6   6   7

询问

SELECT Name,Max(A) AS A,MAX(B) AS B,MAX(C) AS C,MAX(D) AS D
FROM <your table>
GROUP BY Name

预期产出

Name    A   B   C   D
jack    5   3   2   4
john    5   6   6   7
paul    5   7   9   2
于 2012-09-25T07:24:21.800 回答
0

改用GROUP BY

SELECT a.*
FROM items a
     INNER JOIN ( SELECT MIN(id) id
                  FROM items
                  GROUP BY name
                ) b
                ON a.id = b.id
ORDER BY a.name;

示例:SQLFiddle

于 2012-09-25T07:03:09.100 回答
0

试试这个查询

select distinct name,A,B,C,D from items group by name order by name
于 2012-09-25T07:04:59.483 回答
0

试试这个:

如果您有重复的名称,这将选择任何一条记录

   select slno,name,A,B,C,D
   from           
   ( SELECT  @i:=@i+1 AS slno,t.*
     FROM items t ,(SELECT @i:=0) r )a
   group  by    name  
   having slno=min(slno)


SQL 小提琴演示

于 2012-09-25T07:16:37.323 回答