dalianliyan
6/28/2017 - 3:57 AM

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

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


Id  name  age  salary
1   yzk    80  1000
2   yzk    80  2000
3   tom    20  20000
4   tom    20  20000
5   im     20  20000

//取得不重复的数据

select * from Persons
where Id in
(
    SELECT     MAX(Id) AS Expr1
    FROM         Persons
    GROUP BY Name, Age
)

//根据姓名、年龄分组,取出每组的Id最大值,然后将Id最大值之外的排除。删除重复的数据:

delete from Persons
where Id not in
(
SELECT     MAX(Id) AS Expr1
FROM         Persons
GROUP BY Name, Age
)