日期:2014-05-18  浏览次数:20441 次

sql字段唯一查询
表结构如下
ID    ZiMu   Shuzi
1    A     1
2    B     1
3    C     1
4    A     2
5    B     2
6    C     2
7    A     3
8    B     3
9    C     3

要求查询到以下结果

ID    ZiMu   Shuzi
1    A     1
4    A     2
7    A     3


------解决方案--------------------
SQL code

declare @t table (ID int,ZiMu varchar(1),Shuzi int)
insert into @t
select 1,'A',1 union all
select 2,'B',1 union all
select 3,'C',1 union all
select 4,'A',2 union all
select 5,'B',2 union all
select 6,'C',2 union all
select 7,'A',3 union all
select 8,'B',3 union all
select 9,'C',3

select * from @t t where ID=(select min(ID) from @t where Shuzi=t.Shuzi)
/*
ID          ZiMu Shuzi
----------- ---- -----------
1           A    1
4           A    2
7           A    3
*/