从表中删除重复的行

时间:2011-05-07 12:52:02

标签: sql postgresql

我的表中有唯一的键ID键,但我有一个重复值的列?我如何摆脱这些,同时只保留其中一个:

重复记录:

id  | name   | surname |
1   | test   | one     |
2   | test   | two     |
3   | test3  | three   |
4   | test7  | four    |
5   | test   | five    |
6   | test11 | eleven  |

没有重复:

id  | name   | surname |
1   | test   | one     |
3   | test3  | three   |
4   | test7  | four    |
6   | test11 | eleven  |

我用谷歌搜索了这个,但似乎没有工作:

DELETE  ct1
FROM    mytable ct1
        , mytable ct2
WHERE   ct1.name = ct2.name 
        AND ct1.id < ct2.id 

ERROR:  syntax error at or near "ct1"
LINE 1: DELETE  ct1
                ^

********** Error **********

我正在使用postgres数据库。

2 个答案:

答案 0 :(得分:3)

您可以尝试运行多次

delete from mytable where id in (
    select max(id)
      from mytable
     group by name
    having count(1) > 1
);

多次等于name列中的最大重复次数。

否则,您可以尝试这个更复杂的查询:

delete from mytable where id in (
    select id from mytable
    except 
    (
    select min(id)
      from mytable
     group by name
    having count(1) > 1
    union all
    select min(id)
      from mytable
     group by name
    having count(1) = 1
    )
);

仅运行此查询一次应删除您需要的所有内容。虽然没试过......

答案 1 :(得分:3)

使用Rank,实际上我并不完全确定语法,因为我在PostgreSQL上并不擅长,这只是一个提示(任何人的修正都会受到赞赏):

DELETE FROM mytable
WHERE id NOT IN
(
   SELECT x.id FROM
   (
      SELECT id, RANK() OVER (PARTITION BY name ORDER BY id ASC) AS r
      FROM mytable
   ) x
   WHERE x.r = 1
)