关于select的问题
id name
1 a
2 b
1 c
2 d
我想查询显示出
id name
1 a
2 b
不知哪位高手帮一下忙
------解决方案--------------------?
Select id , Min(Name) As name From 表 Group By id
------解决方案--------------------举例如下:
create table a(id int,name varchar(10))
insert a values(1, 'a ')
insert a values(2, 'b ')
insert a values(1, 'c ')
insert a values(2, 'd ')
go
declare @tab table(num int identity(1,1),id int,name varchar(10))
insert @tab select * from a
select n.id,n.name
from (
select id,min(num) min_num
from @tab
group by id) m inner join @tab n on m.min_num=n.num
go
返回:
id name
----------- ----------
1 a
2 b
(所影响的行数为 2 行)
------解决方案--------------------create table t(id int,name varchar(10))
insert t values(1, 'a ')
insert t values(2, 'b ')
insert t values(1, 'c ')
insert t values(2, 'd ')
insert t values(1, 'm ')
insert t values(2, 'n ')
select * from t a
where not exists(select 1 from t where id=a.id and name <a.name)
id name
----------- ----------
1 a
2 b
(2 row(s) affected)