PostgreSQL删除重复行忽略大小写
我需要从表中删除在指定字段上具有相同值的行,忽略大小写。例如,如果有一行字段的值为'foo',另一行字段的值为'foo',我只想删除其中一行,保留一行。 我试过这样的方法:PostgreSQL删除重复行忽略大小写,postgresql,case-insensitive,duplicate-removal,Postgresql,Case Insensitive,Duplicate Removal,我需要从表中删除在指定字段上具有相同值的行,忽略大小写。例如,如果有一行字段的值为'foo',另一行字段的值为'foo',我只想删除其中一行,保留一行。 我试过这样的方法: delete from table t1 where exists (select 1 from table t2 where t1.key <> t2.key and t1.field ILIKE t2.field)
delete from table t1
where exists (select 1
from table t2
where t1.key <> t2.key
and t1.field ILIKE t2.field)
但这也会删除另一行。
有什么建议吗?只需更改以保留具有最低键的记录。假设键是表的主键:
DELETE FROM the_table t1
WHERE t1.key not in (select min(t2.key)
from the_table t2
group by lower(t2.field));
DELETE FROM the_table t1
WHERE t1.key not in (select min(t2.key)
from the_table t2
group by lower(t2.field));