MySQL 处理 ÅÄÖ作为AAO?

发布于 2024-08-28 10:16:03 字数 239 浏览 5 评论 0原文

这两个查询给了我完全相同的结果:

select * from topics where name='Harligt';
select * from topics where name='Härligt';

这怎么可能?似乎 mysql 在搜索时将 åäö 翻译为 aao 。有什么办法可以关掉这个吗?

据我所知,我到处都使用 utf-8 编码。终端和 php 都会出现同样的问题。

These two querys gives me the exact same result:

select * from topics where name='Harligt';
select * from topics where name='Härligt';

How is this possible? Seems like mysql translates åäö to aao when it searches. Is there some way to turn this off?

I use utf-8 encoding everywhere as far as i know. The same problem occurs both from terminal and from php.

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

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

发布评论

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

评论(5

盛夏尉蓝 2024-09-04 10:16:03

是的,这是非特定于语言的 unicode 排序规则中的标准行为。

9.1.13.1。 Unicode 字符集

为了进一步说明,以下等式在 utf8_general_ci 和 utf8_unicode_ci 中均成立(有关比较或搜索时的效果,请参阅第 9.1.7.7 节“排序规则效果的示例”):

A = A
Ø = O
Ü = U

另请参见排序规则效果示例

您需要

  • 使用不具有此“功能”的排序规则(即utf8_bin,但这会产生其他后果)

  • 仅针对查询使用不同的排序规则。这应该有效:

     从 name='Harligt' 的主题中选择 * COLLATE utf8_bin;
    

如果您想要执行不区分大小写的 LIKE进行 Ä = A 变音转换,则会变得更加困难。我知道没有任何 mySQL 排序规则是不区分大小写的,并且不会执行这种隐式变音转换。如果有人知道的话,我很想听听。

相关:

Yes, this is standard behaviour in the non-language-specific unicode collations.

9.1.13.1. Unicode Character Sets

To further illustrate, the following equalities hold in both utf8_general_ci and utf8_unicode_ci (for the effect this has in comparisons or when doing searches, see Section 9.1.7.7, “Examples of the Effect of Collation”):

Ä = A
Ö = O
Ü = U

See also Examples of the effect of collation

You need to either

  • use a collation that doesn't have this "feature" (namely utf8_bin, but that has other consequences)

  • use a different collation for the query only. This should work:

     select * from topics where name='Harligt' COLLATE utf8_bin;
    

it becomes more difficult if you want to do a case insensitive LIKE but not have the Ä = A umlaut conversion. I know no mySQL collation that is case insensitive and does not do this kind of implicit umlaut conversion. If anybody knows one, I'd be interested to hear about it.

Related:

我不会写诗 2024-09-04 10:16:03

由于您在瑞典,我建议使用瑞典语排序规则。下面是一个显示其差异的示例:

CREATE TABLE topics (name varchar(100) not null) CHARACTER SET utf8;

INSERT topics (name) VALUES ('Härligt');

select * from topics where name='Harligt';
'Härligt'

select * from topics where name='Härligt';
'Härligt'    

ALTER TABLE topics MODIFY name VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_swedish_ci;

select * from topics where name='Harligt';
<no results>

select * from topics where name='Härligt';
'Härligt'

请注意,在本示例中,我仅将一列更改为瑞典语排序规则,但您可能应该对整个数据库、所有表、所有 varchar 列执行此操作。

Since you are in Sweden I'd recommend using the Swedish collation. Here's an example showing the difference it makes:

CREATE TABLE topics (name varchar(100) not null) CHARACTER SET utf8;

INSERT topics (name) VALUES ('Härligt');

select * from topics where name='Harligt';
'Härligt'

select * from topics where name='Härligt';
'Härligt'    

ALTER TABLE topics MODIFY name VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_swedish_ci;

select * from topics where name='Harligt';
<no results>

select * from topics where name='Härligt';
'Härligt'

Note that in this example I only changed the one column to Swedish collation, but you should probably do it for your entire database, all tables, all varchar columns.

‖放下 2024-09-04 10:16:03

虽然排序规则是解决此问题的一种方法,但在我看来,更直接的方法是 BINARY 关键字:

 SELECT 'a' = 'ä', BINARY 'a' = 'ä'

将返回 1|0

在您的情况下:

SELECT * FROM topics WHERE BINARY name='Härligt';

另请参阅 https://www.w3schools.com/sql/func_mysql_binary.asp

While collations are one way of solving this, the much more straightforward way seems to me to be the BINARY keyword:

 SELECT 'a' = 'ä', BINARY 'a' = 'ä'

will return 1|0

In your case:

SELECT * FROM topics WHERE BINARY name='Härligt';

See also https://www.w3schools.com/sql/func_mysql_binary.asp

脸赞 2024-09-04 10:16:03

您想要检查排序规则设置,排序规则是设置哪些字符相同的属性。

这两个页面应该可以帮助您

http://dev.mysql。 com/doc/refman/5.1/en/charset-general.html

http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html

you want to check your collation settings, collation is the property that sets which characters are identical.

these 2 pages should help you

http://dev.mysql.com/doc/refman/5.1/en/charset-general.html

http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html

岁月如刀 2024-09-04 10:16:03

在这里你可以看到一些整理图表。 http://collat​​ion-charts.org/mysql60/。我不确定哪个是使用的 utf8_general_ci 。

这是 utf8_swedish_ci 的图表。它显示它解释为相同的字符。 http://collat​​ion-charts.org/mysql60/mysql604.utf8_swedish_ci.html

Here you can see some collation charts. http://collation-charts.org/mysql60/. I'm no sure which is the used utf8_general_ci though.

Here is the chart for utf8_swedish_ci. It shows which characters it interprets as the same. http://collation-charts.org/mysql60/mysql604.utf8_swedish_ci.html

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