MYSQL区分大小写搜索utf8_bin字段

发布于 2024-07-22 11:52:30 字数 597 浏览 7 评论 0原文

我创建了一个表并将排序规则设置为 utf8,以便能够向字段添加唯一索引。 现在我需要进行不区分大小写的搜索,但是当我使用 collat​​e 关键字执行一些查询时,我得到:

mysql> select * from page where pageTitle="Something" Collate utf8_general_ci;

错误 1253 (42000):排序规则“utf8_general_ci”对于以下内容无效 字符集“latin1”

mysql> select * from page where pageTitle="Something" Collate latin1_general_ci;

错误 1267 (HY000):非法混合排序规则(utf8_bin、IMPLICIT)和 (latin1_general_ci,EXPLICIT) 用于操作“=”

我对 SQL 很陌生,所以我想知道是否有人可以提供帮助。

I created a table and set the collation to utf8 in order to be able to add a unique index to a field. Now I need to do case insensitive searches, but when I performed some queries with the collate keyword and I got:

mysql> select * from page where pageTitle="Something" Collate utf8_general_ci;

ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for
CHARACTER SET 'latin1'

mysql> select * from page where pageTitle="Something" Collate latin1_general_ci;

ERROR 1267 (HY000): Illegal mix of collations (utf8_bin,IMPLICIT) and
(latin1_general_ci,EXPLICIT) for operation '='

I am pretty new to SQL, so I was wondering if anyone could help.

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

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

发布评论

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

评论(4

耳根太软 2024-07-29 11:52:30

MySQL 中的字符串具有字符集和排序规则。 utf8 是字符集,utf8_bin 是其排序规则之一。 要将字符串文字与 utf8 列进行比较,请通过在其前面加上 _charset 表示法将其转换为 utf8:

_utf8 'Something'

现在排序规则仅对某些字符集有效。 utf8 区分大小写的排序规则似乎是 utf8_bin,您可以像这样指定它:

_utf8 'Something' collate utf8_bin

通过这些转换,查询应该可以工作:

select * from page where pageTitle = _utf8 'Something' collate utf8_bin

_charset 前缀适用于字符串文字。 要更改字段的字符集,可以使用 CONVERT ... USING。 当您想要将 pageTitle 字段转换为另一个字符集时,这非常有用,例如:

select * from page 
where convert(pageTitle using latin1) collate latin1_general_cs = 'Something'

要查看名为“TAB”的表中名为“col”的列的字符和排序规则,请尝试:

select distinct collation(col), charset(col) from TAB

所有字符集的列表可以通过以下方式找到排序规则:

show character set
show collation

可以通过以下方式找到 utf8 的所有有效排序规则:

show collation where charset = 'utf8'

A string in MySQL has a character set and a collation. Utf8 is the character set, and utf8_bin is one of its collations. To compare your string literal to an utf8 column, convert it to utf8 by prefixing it with the _charset notation:

_utf8 'Something'

Now a collation is only valid for some character sets. The case-sensitive collation for utf8 appears to be utf8_bin, which you can specify like:

_utf8 'Something' collate utf8_bin

With these conversions, the query should work:

select * from page where pageTitle = _utf8 'Something' collate utf8_bin

The _charset prefix works with string literals. To change the character set of a field, there is CONVERT ... USING. This is useful when you'd like to convert the pageTitle field to another character set, as in:

select * from page 
where convert(pageTitle using latin1) collate latin1_general_cs = 'Something'

To see the character and collation for a column named 'col' in a table called 'TAB', try:

select distinct collation(col), charset(col) from TAB

A list of all character sets and collations can be found with:

show character set
show collation

And all valid collations for utf8 can be found with:

show collation where charset = 'utf8'
帅哥哥的热头脑 2024-07-29 11:52:30

另请注意,如果使用“Collat​​e utf8_general_ci”或“Collat​​e latin1_general_ci”,即“强制”整理 - 这样的转换将阻止使用现有索引! 这可能会成为未来性能的瓶颈。

Also please note that in case of using "Collate utf8_general_ci" or "Collate latin1_general_ci", i.e. "force" collate - such a converting will prevent from usage of existing indexes! This could be a bottleneck in future for performance.

提笔书几行 2024-07-29 11:52:30

试试这个,它对我有用

SELECT * FROM users WHERE UPPER(name) = UPPER('josé') COLLATE utf8_bin;

Try this, Its working for me

SELECT * FROM users WHERE UPPER(name) = UPPER('josé') COLLATE utf8_bin;

我一向站在原地 2024-07-29 11:52:30

请问为什么在执行 SELECT 时需要显式更改排序规则? 为什么不按照您想要在排序时检索记录的方式进行整理呢?

您的搜索区分大小写时遇到的问题是您有二进制排序规则。 尝试使用通用排序规则。 有关区分大小写和排序规则的更多信息,请查看此处:
字符串搜索中的区分大小写

May I ask why you have a need to explicitly change the collation when you do a SELECT? Why not just collate in the way you want to retrieve the records when sorted?

The problem you are having with your searches being case sensitive is that you have a binary collation. Try instead to use the general collation. For more information about case sensitivity and collations, look here:
Case Sensitivity in String Searches

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