删除重复的条目

时间:2019-03-15 11:11:37

标签: mysql

如何在不使用distinctgroup by的情况下从数据库中删除重复的条目

Id.        Name              age         dob
1.           b.               11.        1992
2.           d.               14.        1967
3.           b.               11.        1992

2 个答案:

答案 0 :(得分:1)

您可以尝试以下操作: 假设test是您的表名:-

delete th 
from test as th 
where th.id IN (select id 
                from (select a.id  
                      from test as a 
                      left join test as b 
                          on a.name = b.name where a.id>b.id) AS T);

答案 1 :(得分:1)

您提到使用Name基于ROW_NUMBER()进行重复输入时,可以不用DISTINCTGROUP BY

查询将是:

DELETE FROM (
    SELECT Id, ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Id) AS RN
    FROM Table
) AS Q
WHERE RN = 1