SQL删除/更新与子查询
使用下面的SELECT查询作为子查询,我想更新我的表,以便删除重复项。具体来说,我想删除ID为2和5的记录 数据:SQL删除/更新与子查询,sql,postgresql,Sql,Postgresql,使用下面的SELECT查询作为子查询,我想更新我的表,以便删除重复项。具体来说,我想删除ID为2和5的记录 数据: id |电子邮件 ------------------------- 1|@example.com 2 |@example.com 3 |@test.com 4|@.example.net 5 |@example.net count | addr -------+------------------------- 2 | example.com 2 | example.net 您
id |电子邮件
-------------------------
1|@example.com
2 |@example.com
3 |@test.com
4|@.example.net
5 |@example.net
count | addr
-------+-------------------------
2 | example.com
2 | example.net
您的示例SQL引用了两个表,因此它似乎与您的实际问题无关
如果只有一个表,则逻辑(在大多数数据库中)如下所示:
delete from data
where id > (select min(d2.id)
from data d2
where replace(replace(d2.email, '@.', '' ), '@', '' ) = replace(replace(data.email, '@.', '' ), '@', '' )
);
用您正在使用的数据库标记您的问题。您还显示了一个表中的值,但您的问题涉及两个表。
delete from data
where id > (select min(d2.id)
from data d2
where replace(replace(d2.email, '@.', '' ), '@', '' ) = replace(replace(data.email, '@.', '' ), '@', '' )
);