postgres=# create table test(id int,name varchar); CREATE TABLE postgres=# insert into test values (1,'kenyon'); INSERT 0 1 postgres=# insert into test values (1,'kenyon'); INSERT 0 1 postgres=# insert into test values (2,'kenyon_test'); INSERT 0 1 postgres=# insert into test values (2,'kenyon_test'); INSERT 0 1 postgres=# insert into test values (3,'test'); INSERT 0 1 postgres=# insert into test values (5,'jackson'); INSERT 0 1 postgres=# select ctid,* from test; ctid | id | name -------+----+------------- (0,1) | 1 | kenyon (0,2) | 1 | kenyon (0,3) | 1 | kenyon (0,4) | 2 | kenyon_test (0,5) | 2 | kenyon_test (0,6) | 3 | test (0,7) | 5 | test (0,8) | 5 | jackson (8 rows)查询要保留的数据,以min(ctid)或max(ctid)为准
postgres=# select ctid,* from test where ctid in (select min(ctid) from test group by id); ctid | id | name -------+----+------------- (0,7) | 5 | test (4 rows)删除重复数据,查看最后结果
postgres=# delete from test where ctid not in (select min(ctid) from test group by id); DELETE 4 postgres=# select ctid,7) | 5 | test (4 rows)如果表中已经有标明唯一的序列主键值,可以把该值替换上述的ctid直接删除。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。