日期:2014-05-19  浏览次数:20443 次

如何查找不重复的记录
比如一个表(T)有以下字段及内容:
ID                   NAME
1                     A
2                     B
3                     C
3                     CCCCC
4                     D
5                     E
5                     EEE
通过一个条查询语句后我想要得到的结果为    
ID                   NAME
1                     A
2                     B
3                     C
4                     D
5                     E
请问只用一条SQL语句怎么写呀?


------解决方案--------------------
create table test(ID int,NAME varchar(10))
insert test select 1, 'A '
union all select 2, 'B '
union all select 3, 'C '
union all select 3, 'CCCCC '
union all select 4, 'D '
union all select 5, 'E '
union all select 5, 'EEE '


select * from test a where NAME=
(
select top 1 NAME from test where ID=a.ID
)
------解决方案--------------------
select ID,min(Name) as Name from 表 group by ID
------解决方案--------------------
select id,min(NAME) from T group by id ?
------解决方案--------------------
select ID,min(Name) as Name from T group by ID

------解决方案--------------------
--如果仅有这两个字段,直接分组即可:

select
id,
min(name) as name
from 表
group by id
order by id