从特定字段为空的 postgresql 数据库中删除重复项

Delete duplicates from postgresql database where certain field is null

考虑以下 table:

+----+-------+--------------+
| id | name  |     info     |
+----+-------+--------------+
|  1 | Chris | NULL         |
|  2 | Den   | some info    |
|  3 | Bob   | another info |
|  4 | Bob   | NULL         |
+----+-------+--------------+

我想删除字段 name 中的所有重复项,但只删除 infoNULL 的那些。对于这个例子,我只想删除最后一个 Bob (id = 4)。

看来我可以 select 他们:

select name from mytable where info is null group by name having count(*) > 1;

但我无法检查它是否正确,因为添加字段会引发错误:

select id, name from mytable where info is null group by name having count(*) > 1;

ERROR: column "mytable.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: select id, name from mytable where info is null group ...

谢谢。

应该这样做:

delete from mytable 
where info is null
and exists (select *
            from mytable t2 
            where t2.name = mytable.name
              and t2.info is not null)