删除重复记录,并保留ID较低的记录

时间:2017-07-13 19:59:14

标签: sql

id  | MEMBER_BADGE_ID | MEMBER_ID
3127| 859986          | 2320
3128| 859986          | 2320
2461| 860058          | 1915
2545| 860062          | 1921
2546| 860062          | 1921
2460| 860063          | 1164
2468| 860064          | 1919
2469| 860064          | 1919 
2472| 860067          | 1921
2476| 860076          | 1923
2547| 860077          | 1930
2489| 860086          | 1930
2474| 860089          | 1922
2475| 860089          | 1922
2477| 8600102         | 1924

我想删除整个表中最旧ID的重复行。

select id,MEMBER_BADGE_ID,MEMBER_ID
from MEMBERS
order by MEMBER_BADGE_ID

1 个答案:

答案 0 :(得分:1)

这个:

    DELETE FROM MEMBERS 
    WHERE id IN (
    SELECT MIN(id) id
    FROM MEMBERS 
    group by MEMBER_BADGE_ID, MEMBER_ID 
    having count(*) >1)