SQL去重

如今有一张表t(id,name),id是主键,name能够重复,如今要删除重复数据,保留id最小的数据。请写出SQL。html

表:tsql

id        namepost

1        张三url

2        张三spa

3        李四code

4        李四htm

5        李四blog

分析:get

首先经过名字分组,选出每组id最小记录。而后删除这些记录之外的全部数据。it

1:select min(id) id,name from t groud by name.

重点:min(),groud by, not exists()

完整的SQL:

 

delete from t a where not exists( select * from ( select min(id) ,name from t group by name) b where a.id=b.id)
相关文章
相关标签/搜索