如何在 Drupal 中对带有变音符号的文本进行 MySQL 查询?

发布于 2024-09-19 09:21:11 字数 357 浏览 7 评论 0原文

我需要区分以“O”开头的节点和以“Ö”(o 元音变音)开头的节点。

问题是,由于节点表和标题列具有 utf8_general_ci 排序规则,MYSQL 不会以不同方式对待 o 和 ö 元音变音,并且此查询返回以 O 开头的节点 AND 以 Ö 开头的节点

SELECT node.nid AS nid
FROM节点AS节点
WHERE 节点.status <> 0
AND SUBSTR( node.title, 1, 1 ) = 'O'

由于所有 Drupal 模块都使用 utf8_general_ci 排序规则,我想更改表和整个数据库的排序规则可能不是一个好主意。

对此有什么好的解决方法吗?

I need to differentiate between nodes starting with 'O' and nodes starting with 'Ö' (o umlaut).

The problem is that since the node table and the title column have utf8_general_ci collation, MYSQL does not treat o and ö umlaut differently and this query returns nodes starting with O AND nodes starting with Ö

SELECT node.nid AS nid
FROM node AS node
WHERE node.status <> 0
AND SUBSTR( node.title, 1, 1 ) = 'O'

Since all Drupal modules use utf8_general_ci collation, I guess changing the collation of the table and entire database is probably not a good idea.

What is a good workaround for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-09-26 09:21:11

您可以使用与列所使用的排序规则不同的排序规则进行比较:(

mysql> SELECT 'foo'='föo' COLLATE 'utf8_general_ci';
1
mysql> SELECT 'foo'='föo' COLLATE 'utf8_swedish_ci';
0

oö 是瑞典语排序规则中的不同字母。您也可以使用 utf8_bin(如果您不想匹配任何不同的字符,甚至是同一字母的不同大小写)。)

但是,对排序规则与排序规则不同的列进行比较每列都意味着不能使用索引。因此,只需将表ALTER更改为您想要的更具体的排序规则可能会更有效。

You can do a comparison using a different collation to the one the columns are using:

mysql> SELECT 'foo'='föo' COLLATE 'utf8_general_ci';
1
mysql> SELECT 'foo'='föo' COLLATE 'utf8_swedish_ci';
0

(o and ö being different letters in the Swedish collation. You could also use utf8_bin if you don't want any different characters to match, even different cases of the same letter.)

However doing a compare of columns where the collation is not the same as the collation of each column means indexes can't be used. So it may be more efficient simply to ALTER the tables to the more-specific collation you want.

寄居者 2024-09-26 09:21:11

不幸的是,正如您所注意到的,Drupal 6 强制执行 utf8_general_ci 排序规则。

然而,这是一个已知问题,人们一直致力于允许在 settings.php 中指定排序规则:不遵守数据库默认排序规则

当前该问题中的补丁 (#90)审查中的 Drupal 6 添加了此功能。修补后,您所需要做的就是将: 添加

$db_collation = 'utf8_swedish_ci`;

到您的 settings.php 文件中。

Unfortunately, as you've noticed, Drupal 6 enforces utf8_general_ci collation.

However, this is a known issue, and people have been working on allowing the collation to be specified in settings.php: Database default collation is not respected

There's currently a patch in that issue (#90) for Drupal 6 in review that adds this ability. Once patched, all you need to do is add:

$db_collation = 'utf8_swedish_ci`;

to your settings.php file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文