现有记录:
id  name 
1   a
2   b
3   a
4   c
5   d
6   b
7   c
要求按名称分组显示如:
1 a
3 a
2 b
6 b
4 c
7 c
请问sql如何写?

解决方案 »

  1.   

    select * from 表 order by name
      

  2.   

    declare @tb table (id int,name varchar(50))
    insert into @tb select 1,'a'
    insert into @tb select 2,'b'
    insert into @tb select 3,'a'
    insert into @tb select 4,'c'
    insert into @tb select 5,'d'
    insert into @tb select 6,'b'
    insert into @tb select 7,'c'select * from @tb t where exists(
    select 1 from @tb where name=t.name
    group by name
    having count(1)>1
    )
    order by name,idid name
    1 a
    3 a
    2 b
    6 b
    4 c
    7 c
      

  3.   

    select * from 表 order by name,id
      

  4.   

    如果名字不规则
    id name spec val
    1  butt 09_1  1
    2  yve  23_1  0
    3  huy  U_2   1
    4  butt 09_1  1
    5  huy  U_2   1
    6  ccer Nb_21 1
    按照自动name进行分组排序?