我有一个包含重复项的表,我希望每个重复项只保留一行。
我可以使用SQL命令选择重复项:
SELECT DISTINCT ON (email, first_name, last_name) * from customer;
但是我想在先前的命令中使用DELETE
。
此命令应该正常工作吗?
DELETE FROM customer WHERE customer.id NOT IN
(SELECT id FROM
(SELECT DISTINCT ON (email, first_name, last_name) * from customer));
是真的吗?
答案 0 :(得分:2)
我想您有一个id
字段。
delete from customer
where id not in (
select min(id)
from customer
group by email, first_name, last_name
)
子查询找到要保留的行的ID。 然后删除其他行
答案 1 :(得分:1)
ID
中找不到您的(SELECT DISTINCT ON (email, first_name, last_name) * from customer));
distinct on
仅返回不可预测的复制数据的第一行