【发布时间】:2020-01-13 16:30:23
【问题描述】:
【问题讨论】:
-
描述您要删除的值。
-
为了安全删除refer this
-
我要删除第 2 条记录、第 4 条记录、第 7 条记录、第 11 条记录和第 13 条记录。 (见图)
标签: sql postgresql duplicates
【问题讨论】:
标签: sql postgresql duplicates
我想你是在问这个:
DELETE FROM tablename
WHERE id IN (SELECT id
FROM (SELECT id,
ROW_NUMBER() OVER (partition BY column1, column2, column3 ORDER BY id) AS rnum
FROM tablename) t
WHERE t.rnum > 1);
【讨论】:
translationset_id 和key 列上,而不是其他列。
您似乎只想删除与translationset_id 列重复的记录。在这种情况下,我们可以使用 Postgres 的行号功能来区分重复的行,然后删除那些重复的行。
WITH cte AS
(
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY translationset_id, key) AS rnum
FROM yourTable t
)
DELETE FROM yourTable
WHERE translationset_id IN (SELECT translationset_id FROM cte WHERE rnum > 1)
【讨论】:
delete from mytable
where exists (select 1
from mytable t2
where t2.name = mytable.name and
t2.address = mytable.address and
t2.zip = mytable.zip and
t2.ctid > mytable.ctid
);
【讨论】:
我认为最有效的方法如下。
DELETE FROM
table_name a
USING table_name b
WHERE
a.id < b.id and
a.same_column = b.same_column;
【讨论】: