删除姓名、年龄重复的记录——数据库

删除姓名、年龄重复的记录

Id name age salary
1 yzk 80 1000
2 yzk 80 2000
3 tom 20 20000
4 tom 20 20000
5 im 20 20000
a. 使用Distinct 去重:
SELECT DISTINCT Name FROM Persons(Distinct 只能去除结果集中一模一样的数据)
b. 获取不重复数据思考1:
SELECT Name, age FROM Persons GROUP BY Name, Gender
c. 获取不重复数据思考2:
SELECT MAX(Id) AS Expr1, Name, Gender FROM Persons GROUP BY Name, age
d. 获取不重复数据:
select * from Persons where Id in ( SELECT MAX(Id) AS Expr1 FROM Persons GROUP BY Name, age )
e. 删除重复的数据:
delete from Persons where Id not in ( SELECT MAX(Id) AS Expr1 FROM Persons GROUP BY Name, age )

    原文作者:叮咚【已开双封】
    原文地址: https://blog.csdn.net/jiurenhuan/article/details/111696851
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞