当一个列有重音字符时,如何 SQL 比较列?
我有两个 SQLite 表,我希望将它们连接到名称列上。此列包含重音字符,所以我想知道如何比较它们以进行连接。我希望去掉重音以便进行比较。
I have two SQLite tables, that I would love to join them on a name column. This column contains accented characters, so I am wondering how can I compare them for join. I would like the accents dropped for the comparison to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用排序规则影响字符的比较(例如忽略大小写、忽略重音)。 SQLLite 仅具有一些内置排序规则,但您可以添加自己的排序规则。
编辑:
鉴于 Android 是否支持 UDF 和计算列似乎值得怀疑,这里有另一种方法:
由于规范化函数现在是在 java 中,因此在编码时应该没有什么限制。 此处给出了在 java 中删除重音符号的几个示例。
You can influence the comparison of characters (such as ignoring case, ignoring accents) by using a Collation. SQLLite has only a few built in collations, although you can add your own.
EDIT:
Given that it seems doubtful if Android supports UDFs and computed columns, here's another approach:
As the normalization function is now in java, you should have few restrictions in coding it. Several examples for removing accents in java are given here.
有一个简单的解决方案,但不是很优雅。
使用 REPLACE 函数删除重音符号。示例:
其中 SEARCH_KEY 是您想要在列上查找的关键字。
There is an easy solution, but not very elegant.
Use the REPLACE function, to remove your accents. Exemple:
Where SEARCH_KEY is the key word that you wanna find on the column.
正如 mdma 所说,一个可能的解决方案是用户定义函数 (UDF)。 有一个文档这里描述了如何在PHP中为SQLite创建这样的函数。您可以编写一个名为 DROPACCENTS() 的函数,该函数会删除字符串中的所有重音符号。然后,您可以使用以下代码连接您的列: 与
使用
UCASE()
函数执行不区分大小写的连接的方式非常相似。由于您无法在 Android 上使用 PHP,因此您必须找到另一种方法来创建 UDF。尽管据说创建 UDF 在 Android 上是不可能的,还有另一篇 Stack Overflow 文章声称 内容提供商可以做到这一点。后者听起来有点复杂,但很有希望。
As mdma says, a possible solution would be a User-Defined-Function (UDF). There is a document here describing how to create such a function for SQLite in PHP. You could write a function called
DROPACCENTS()
which drops all the accents in the string. Then, you could join your column with the following code:Much similar to how you would use the
UCASE()
function to perform a case-insensitive join.Since you cannot use PHP on Android, you would have to find another way to create the UDF. Although it has been said that creating a UDF is not possible on Android, there is another Stack Overflow article claiming that a content provider could do the trick. The latter sounds slightly complicated, but promising.
存储一个不带重音字符的特殊“中性”列,并仅比较/搜索该列。
Store a special "neutral" column without accented characters and compare / search only this column.