从PostgreSQL删除重复项

时间:2019-07-02 11:26:18

标签: sql postgresql postgresql-10

我正在query中使用以下PostgreSQL 10查找重复条目:

select
  column1, column2, count(*)
from mytable
  where column3 in ('yes', 'no')
group by column1, column2 having count(*) > 2;

除了每个条目的第一个条目外,是否有可能PostgreSQL删除重复项?

1 个答案:

答案 0 :(得分:1)

假设您的表具有主键:

delete from mytable t
    where t.pk <> (select min(t2.pk)
                   from mytable t2
                   where t2.column1 = t.column1 and
                         t2.column2 = t.column2 and
                         t2.column3 in ('yes', 'no')
                  );