我有一个表字段如下:
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
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
where not exists (select 1 from tablename where xh = a.xh and id < a.id)
where not exists (select 1 from tablename where xh = a.xh and id < a.id)哪个比较快?
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)
中间还有很多其他条件,怎么写比较可以
====================================说明具体条件
where id in (select min(id) from #tab group by xh)