我有一个表字段如下:
id(int 自增),xh(varchar(6) ),kc(varchar(6))表内的记录如下:
id           xh              kc
1          001               01
2          002               01
3          001               02
4          001               03
5          002               05我怎么能用一条SQL语句得到每个xh在这个表中的id最小的记录。得到的字段是xh和kc

解决方案 »

  1.   

    select * from tablename a
    where not exists (select 1 from tablename where xh = a.xh and id < a.id)
      

  2.   

    select xh,kc from 表名 where id in (select max(id ) id from 表名 group by xh)
      

  3.   

    select xh,kc from tablename where id in(select min(id) from tablename group by xh)
      

  4.   

    select xh,kc from tablename where id in(select min(id) from tablename group by xh)select * from tablename a
    where not exists (select 1 from tablename where xh = a.xh and id < a.id)哪个比较快?
      

  5.   

    txaywzc(太虚遨游) ( ) 信誉:100    Blog  2007-01-09 15:35:08  得分: 0  
     
       select xh,kc from tablename where id in(select min(id) from tablename group by xh)select * from tablename a
    where not exists (select 1 from tablename where xh = a.xh and id < a.id)哪个比较快?
      =============================================
     --一般认为这个比较好一些
    select * from tablename a
    where not exists (select 1 from tablename where xh = a.xh and id < a.id)
      

  6.   

    txaywzc(太虚遨游) ( ) 信誉:100    Blog  2007-01-09 15:37:28  得分: 0  
     
       中间还有很多其他条件,怎么写比较可以
      
     ====================================说明具体条件
      

  7.   

    select xh ,kc from #tab a
    where id in (select min(id)  from #tab group by xh)
      

  8.   

    select xh,kc from (select min(id),xh,kc from #table group xh,id,kc)