Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL删除重复行忽略大小写_Postgresql_Case Insensitive_Duplicate Removal - Fatal编程技术网

PostgreSQL删除重复行忽略大小写

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)

我需要从表中删除在指定字段上具有相同值的行,忽略大小写。例如,如果有一行字段的值为'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 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));