PostgreSQL 删除重复行忽略大小写
我需要从表中删除在指定字段上具有相同值的行,忽略大小写。例如,如果我有一行将“foo”作为字段值,另一行将“Foo”作为同一字段的值,我只想删除其中一行(保留 1 行)。
我尝试过类似的操作:
delete from table t1
where exists (select 1
from table t2
where t1.key <> t2.key
and t1.field ILIKE t2.field)
但这也会删除另一行。
有什么建议吗?
I need to delete from a table rows that have the same value on a specified field ignoring case. For example if I have a row that has 'foo' as value for a field and another row that has 'Foo' as value for the same field, I want to delete only one of these rows (keeping 1 row).
I've tried something like this:
delete from table t1
where exists (select 1
from table t2
where t1.key <> t2.key
and t1.field ILIKE t2.field)
but this deletes the other row too.
Are there any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将
<>
更改为<
:这样您就可以保留具有最高
key
的行。您还可以使用>
来保留具有最低key
的记录。Just change
<>
by<
:This way you keep the rows with the highest
key
. You also could use>
to keep the records with the lowestkey
.假设
key
是表的主键:Assuming
key
is the primary key of the table: