PostgreSQL:如何删除表中的重复行?

您有一张表,并且想要删除表中所有在 col2 上的重复条目。col1 作为主键。

1. 使用 USING 扩展:

DELETE FROM table_name USING table_name temp_table
	WHERE table_name.col2 = temp_table.col2 AND table_name.col1 < temp_table.col1;

2. SELECT DISTINCT 之后,DROP TABLE

CREATE TABLE temp_table (LIKE table_name);
INSERT INTO temp_table SELECT DISTINCT * FROM table_name;
DROP TABLE table_name;
ALTER TABLE temp_table RENAME TO table_name;

TablePlus 是 MySQL、PostgreSQL、SQLite 和 Microsoft SQL Server 等数据库的原生客户端。您可以同时打开多个数据库。

下载适用于 Mac 的 TablePlus.

不在 Mac 上? 下载 TablePlus for Windows.

使用 Linux? 下载适用于 Linux 的 TablePlus

需要在外出时快速编辑数据? 下载适用于 iOS 的 TablePlus.

TablePlus 中的 PostgreSQL 数据表

← 所有文章