表A:
ID     NAME
1       a
2       b
3       a
4       b
5       c
求一SQL语句,获得结果如下:
ID     NAME
1       a
2       b
5       c
NAME字段无重复,NAME中相同记录时取ID最小的那条.

解决方案 »

  1.   

    selelect id,name from a as a1 where not exists(select id from a where id <a1.id)
      

  2.   

    select min(ID),name from 表A
    group by name
      

  3.   

    直接用group by name就可以了,具體語句:select min=min(id),name from 表A group by name/*-- 結果   
    min         name       
    ----------- ---------- 
    1           a
    2           b
    5           c(所影响的行数为 3 行)
    --*/
      

  4.   

    --建立測試環境
    Declare @Tb table(ID int,Name varchar(10))
    insert @Tb select 1 ,'a' 
    union all select 2 ,'b'
    union all select 3,'a'
    union all select 4,'b'
    union all select 5,'c'select * from @Tb
    --語句
    select min=min(id),name from @Tb group by name
      

  5.   

    select min(id) as ID,name from 表A group by name
      

  6.   

    select * from 表A not exists(select 1 from 表A a where a.id<id and name=a.name
    )
      

  7.   

    上边的有问题 select * from test a where not exists(select 1 from test where id<a.id and names=a.names
    )
      
      

  8.   

    Select Min(ID)As ID ,NAME
    From A
    Group By NAME
      

  9.   

    Select Min(ID),NAME
    From A
    Group By NAME
    order by id