if object_id('td_species') is not null
drop table td_species;
create table td_species([id] int,sp_name varchar(20));
insert into td_species 
select 1, 'rice' union
select 2, 'maize'union
select 3, 'wheat' union
select 4, 'wheat';
select * from td_species;id      sp_name
-----------------------------
1       rice
2       maize
3       wheat
4       wheat
我现在想用一条select语句想输出这样的结果,我应该怎么写?id      sp_name
-----------------------------
1       rice
2       maize
3       wheat
即:我想把sp_name列重复的去除,并且输出id的编号.

解决方案 »

  1.   

    select min(id) ,rice from td_species group by rice
      

  2.   

    Select * From td_species A
    Where (Select Count(1) From td_species Where sp_name= A.sp_name And id> A.id) < 1
      

  3.   

    select * from td_species where id in(select min(id) from td_species group by sp_name)
      

  4.   

    select * from td_species A where id in (select top 1 B.id from td_species B where B. sp_name = A. sp_name)
      

  5.   

    select min(id) ,rice from td_species group by rice
    我说这个呵呵
      

  6.   

    這種寫法好多,通常exists,not exists ,group by 等