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

删除表中重复值,在线等
题目如下:
有一个cj表如下:
id               name             score
1                   a   2
2                   h                   2
3                   t   2
4                   a   2
5                   h   2

问题:
用sql语句实现删除名字重复记录
结果如下:
id               name             score
1                   a   2
2                   h                   2
3                   t   2


------解决方案--------------------

create table T(id int, name char(1), score int)
insert T select 1, 'a ', 2
union all select 2, 'h ', 2
union all select 3, 't ', 2
union all select 4, 'a ', 2
union all select 5, 'h ', 2


delete T
where id not in(
select min(id) from T group by name
)

select * from T

--result
id name score
----------- ---- -----------
1 a 2
2 h 2
3 t 2

(3 row(s) affected)