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)
相關文章
相關標籤/搜索