gpt4 book ai didi

mysql - 如何在 MySQL 中处理引号和撇号以进行字符串比较,以便它们匹配(整理)

转载 作者:可可西里 更新时间:2023-11-01 08:56:06 26 4
gpt4 key购买 nike

MySQL 使用排序规则进行字符串比较,因为某些字符应该匹配

例子:

SELECT 'é' = 'e' COLLATE utf8_unicode_ci;
SELECT 'oe' = 'œ' COLLATE utf8_unicode_ci;

都返回真

现在,我怎样才能对引号 (') 和撇号 (') 做同样的事情

这不是同一个字符,写“it's”或“l'oiseau”(法语)时正确使用的字符都是撇号。

事实是 utf8_general_ci 或 utf8_unicode_ci 都不整理它们。

简单的解决方案是将所有内容存储在引号中,并在用户进行搜索时替换所有撇号,但这是错误的。

真正的解决方案是创建一个基于 utf8_unicode_ci 的自定义排序规则并将两者标记为等效,但这需要编辑 XML 配置文件并重新启动数据库,这并不总是可行的。

你会怎么做?

最佳答案

自定义排序规则似乎是最合适的,但如果这不可能,也许您可​​以调整搜索以使用正则表达式。它并不完全理想,但在某些情况下可能有用。至少它允许您以正确的格式存储数据(无需替换引号),并且只需对搜索查询本身进行替换:

INSERT INTO mytable VALUES
(1, 'Though this be madness, yet there is method in ''t'),
(2, 'Though this be madness, yet there is method in ’t'),
(3, 'There ’s daggers in men’s smiles'),
(4, 'There ’s daggers in men''s smiles');

SELECT * FROM mytable WHERE data REGEXP 'There [\'’]+s daggers in men[\'’]+s smiles';

+----+--------------------------------------+
| id | data |
+----+--------------------------------------+
| 3 | There ’s daggers in men’s smiles |
| 4 | There ’s daggers in men's smiles |
+----+--------------------------------------+

SELECT * FROM mytable WHERE data REGEXP 'Though this be madness, yet there is method in [\'’]+t';

+----+-----------------------------------------------------+
| id | data |
+----+-----------------------------------------------------+
| 1 | Though this be madness, yet there is method in 't |
| 2 | Though this be madness, yet there is method in ’t |
+----+-----------------------------------------------------+

关于mysql - 如何在 MySQL 中处理引号和撇号以进行字符串比较,以便它们匹配(整理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4384180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com