找到忽略大小写和特殊字符的两列可能的重复项 [英] Find possible duplicates in two columns ignoring case and special characters

查看:183
本文介绍了找到忽略大小写和特殊字符的两列可能的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT COUNT(*), name, number
FROM   tbl
GROUP  BY name, number
HAVING COUNT(*) > 1

有时无法找到小写和大写字母之间的重复。

例如: sunny Sunny 不显示为重复。

那么如何在PostgreSQL中找到所有可能重复的两列。

It sometimes fails to find duplicates between lower case and upper case.
E.g.: sunny and Sunny don't show up as a duplicates.
So how to find all possible duplicates in PostgreSQL for two columns.

推荐答案

lower() / upper()



使用其中一个将字符折叠成大写或大写。特殊字符不受影响:

lower()/ upper()

Use one of these to fold characters to either lower or upper case. Special characters are not affected:

SELECT count(*), lower(name), number
FROM   tbl
GROUP  BY lower(name), number
HAVING count(*) > 1;



uncent()



如果你实际上想忽略变音符号,就像您的意见暗示,安装附加模块 noncent ,它提供了一个文本搜索字典,可以删除重音符和通用功能 uncent()

CREATE EXTENSION unaccent;

使其非常简单:

SELECT lower(unaccent('Büßercafé')) AS norm

结果:

busercafe

这不剥夺非字母。添加 regexp_replace()像@Craig所提到的那样:

This doesn't strip non-letters. Add regexp_replace() like @Craig mentioned for that:

SELECT lower(unaccent(regexp_replace('$s^o&f!t Büßercafé', '\W', '', 'g') ))
                                                                     AS norm

结果:

softbusercafe

您甚至可以创建一个功能索引:

You can even build a functional index on top of that:

  • Does PostgreSQL support "accent insensitive" collations?

这篇关于找到忽略大小写和特殊字符的两列可能的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆