MySql 中的非法混合排序规则错误

发布于 2024-07-30 14:10:13 字数 760 浏览 7 评论 0原文

刚刚从上一个问题中得到了这个答案,它很有效!

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount 
FROM ratings WHERE month='Aug' GROUP BY username HAVING TheCount > 4
ORDER BY TheAverage DESC, TheCount DESC

但是当我将这个额外的位插入其中时,会出现以下错误:

文档 #1267 - 非法混合 排序规则 (latin1_swedish_ci,隐式)和 (latin1_general_ci,隐式)对于 操作“=”

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM 
ratings WHERE month='Aug' 
**AND username IN (SELECT username FROM users WHERE gender =1)**
GROUP BY username HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC

表格为:

id, username, rating, Month

Just got this answer from a previous question and it works a treat!

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount 
FROM ratings WHERE month='Aug' GROUP BY username HAVING TheCount > 4
ORDER BY TheAverage DESC, TheCount DESC

But when I stick this extra bit in it gives this error:

Documentation #1267 - Illegal mix of
collations
(latin1_swedish_ci,IMPLICIT) and
(latin1_general_ci,IMPLICIT) for
operation '='

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM 
ratings WHERE month='Aug' 
**AND username IN (SELECT username FROM users WHERE gender =1)**
GROUP BY username HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC

The table is:

id, username, rating, month

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

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

发布评论

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

评论(20

梦屿孤独相伴 2024-08-06 14:10:13

以下是如何检查哪些列的排序规则错误:

SELECT table_schema, table_name, column_name, character_set_name, collation_name

FROM information_schema.columns

WHERE collation_name = 'latin1_general_ci'

ORDER BY table_schema, table_name,ordinal_position; 

这是修复它的查询:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET latin1 COLLATE 'latin1_swedish_ci';

链接

Here's how to check which columns are the wrong collation:

SELECT table_schema, table_name, column_name, character_set_name, collation_name

FROM information_schema.columns

WHERE collation_name = 'latin1_general_ci'

ORDER BY table_schema, table_name,ordinal_position; 

And here's the query to fix it:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET latin1 COLLATE 'latin1_swedish_ci';

Link

守护在此方 2024-08-06 14:10:13

[MySQL]

在这些(非常罕见)的情况下:

  • 两个表确实需要不同的排序规则类型
  • 值不是来自表,而是来自显式枚举,例如:

    SELECT 1 ASnumbers UNION ALL SELECT 2 UNION ALL SELECT 3

您可以比较使用 CAST 或 CONVERT 转换不同表之间的值:

CAST('my text' AS CHAR CHARACTER SET utf8)

CONVERT('my text' USING utf8)

请参阅 MySQL 网站上的 CONVERT 和 CAST 文档

[MySQL]

In these (very rare) cases:

  • two tables that really need different collation types
  • values not coming from a table, but from an explicit enumeration, for instance:

    SELECT 1 AS numbers UNION ALL SELECT 2 UNION ALL SELECT 3

you can compare the values between the different tables by using CAST or CONVERT:

CAST('my text' AS CHAR CHARACTER SET utf8)

CONVERT('my text' USING utf8)

See CONVERT and CAST documentation on MySQL website.

半城柳色半声笛 2024-08-06 14:10:13

检查每个表的排序规则类型,并确保它们具有相同的排序规则。

之后,还要检查您在操作中使用的每个表字段的排序规则类型。

我遇到了同样的错误,这个技巧对我很有效。

Check the collation type of each table, and make sure that they have the same collation.

After that check also the collation type of each table field that you have use in operation.

I had encountered the same error, and that tricks works on me.

相对绾红妆 2024-08-06 14:10:13

我在 PhpMyadmin 上遇到了同样的错误,并执行了此处指出的对我有用的解决方案

ALTER TABLE table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

非法混合MySQL 排序规则错误
另外,我建议使用 General 而不是 swedish,因为这是默认语言,除非您的应用程序使用瑞典语,否则不要使用该语言。

I was getting this same error on PhpMyadmin and did the solution indicated here which worked for me

ALTER TABLE table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

Illegal mix of collations MySQL Error
Also I would recommend going with General instead of swedish since that one is default and not to use the language unless your application is using Swedish.

拿命拼未来 2024-08-06 14:10:13

我认为你应该转换为utf8

--set utf8 for connection
SET collation_connection = 'utf8_general_ci'
--change CHARACTER SET of DB to utf8
ALTER DATABASE dbName CHARACTER SET utf8 COLLATE utf8_general_ci
--change CHARACTER SET of table to utf8
ALTER TABLE tableName CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

I think you should convert to utf8

--set utf8 for connection
SET collation_connection = 'utf8_general_ci'
--change CHARACTER SET of DB to utf8
ALTER DATABASE dbName CHARACTER SET utf8 COLLATE utf8_general_ci
--change CHARACTER SET of table to utf8
ALTER TABLE tableName CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
摇划花蜜的午后 2024-08-06 14:10:13

我也遇到了同样的错误,但在我的情况下,主要问题是在 where 条件下,我正在检查的参数具有一些未知的隐藏字符 (+%A0)

当 < strong>A0 转换我得到 160 但 160 超出了数据库知道的字符范围,这就是为什么数据库无法将其识别为字符其他原因是我的表列是 varchar

  • 我所做的解决方案是我检查是否有一些类似的字符并在运行sql命令之前删除它们

  • < p>ex:- preg_replace('/\D/', '', $myParameter);

I also got same error, but in my case main problem was in where condition the parameter that i'm checking was having some unknown hidden character (+%A0)

When A0 convert I got 160 but 160 was out of the range of the character that db knows, that's why database cannot recognize it as character other thing is my table column is varchar

  • the solution that I did was I checked there is some characters like that and remove those before run the sql command

  • ex:- preg_replace('/\D/', '', $myParameter);

往日 2024-08-06 14:10:13
  • 检查您的 users.gender 列是否为 INTEGER。
  • 尝试:改变表用户转换为字符集latin1 collat​​e latin1_swedish_ci;
  • Check that your users.gender column is an INTEGER.
  • Try: alter table users convert to character set latin1 collate latin1_swedish_ci;
你爱我像她 2024-08-06 14:10:13

您需要将每列排序规则从 latin1_general_ci 更改为 latin1_swedish_ci

You need to change each column Collation from latin1_general_ci to latin1_swedish_ci

病女 2024-08-06 14:10:13

我在存储过程的 where 子句中遇到了同样的错误。 我发现问题发生在本地声明的变量上,该变量先前由同一个表/列加载。

我解决了将数据转换为单个字符类型的问题。

I got this same error inside a stored procedure, in the where clause. i discovered that the problem ocurred with a local declared variable, previously loaded by the same table/column.

I resolved it casting the data to single char type.

怀里藏娇 2024-08-06 14:10:13

简而言之,这个错误是由 MySQL 尝试对具有不同排序规则设置的两个事物执行操作引起的。 如果您使设置匹配,错误就会消失。 当然,您需要根据数据库的用途为数据库选择正确的设置。

以下是关于在两种非常常见的 utf8 排序规则之间进行选择的一些好建议:utf8_general_ci 和 utf8_unicode_ci 之间有什么区别

如果您使用 phpMyAdmin,您可以通过检查错误消息中提到的表格并检查每列的排序规则类型来系统地执行此操作。 首先,您应该检查数据库的整体排序规则设置 - phpMyAdmin 可以告诉您这一点,并在必要时更改它。 但每个表中的每一列都可以有自己的设置。 通常您会希望所有这些都匹配。

在小型数据库中,这很容易手动完成,并且无论如何,如果您完整阅读错误消息,它通常会将您指向正确的位置。 不要忘记查看带有子表的列的“结构”设置。 当您发现不匹配的排序规则时,您可以直接使用 phpMyAdmin 更改它,无需使用查询窗口。 然后再次尝试您的操作。 如果错误仍然存​​在,请继续查找!

In short, this error is caused by MySQL trying to do an operation on two things which have different collation settings. If you make the settings match, the error will go away. Of course, you need to choose the right setting for your database, depending on what it is going to be used for.

Here's some good advice on choosing between two very common utf8 collations: What's the difference between utf8_general_ci and utf8_unicode_ci

If you are using phpMyAdmin you can do this systematically by working through the tables mentioned in your error message, and checking the collation type for each column. First you should check which is the overall collation setting for your database - phpMyAdmin can tell you this and change it if necessary. But each column in each table can have its own setting. Normally you will want all these to match.

In a small database this is easy enough to do by hand, and in any case if you read the error message in full it will usually point you to the right place. Don't forget to look at the 'structure' settings for columns with subtables in as well. When you find a collation that does not match you can change it using phpMyAdmin directly, no need to use the query window. Then try your operation again. If the error persists, keep looking!

冰火雁神 2024-08-06 14:10:13

这里的问题主要是,只需将字段转换为这样的cast(field as varchar)或cast(fields as date)

The problem here mainly, just Cast the field like this cast(field as varchar) or cast(fields as date)

痞味浪人 2024-08-06 14:10:13

我遇到这个问题不是因为我存储在不同的排序规则中,而是因为我的列类型是 JSON,它是二进制的。

像这样修复它:

select table.field COLLATE utf8mb4_0900_ai_ci AS fieldName

I had this problem not because I'm storing in different collations, but because my column type is JSON, which is binary.

Fixed it like this:

select table.field COLLATE utf8mb4_0900_ai_ci AS fieldName
淡莣 2024-08-06 14:10:13

尽可能使用 ascii_bin ,它将与几乎任何排序规则匹配。
无论如何,用户名很少接受特殊字符。

Use ascii_bin where ever possible, it will match up with almost any collation.
A username seldom accepts special characters anyway.

梦屿孤独相伴 2024-08-06 14:10:13

我对设置为 0 到 1 的字段的集合警告也有同样的问题。所有列集合都是相同的。 我们尝试再次更改集合,但没有解决此问题。

最后,我们将该字段更新为 NULL,然后更新为 1,这解决了收集问题。

I have the same problem with collection warning for a field that is set from 0 to 1. All columns collections was the same. We try to change collections again but nothing fix this issue.

At the end we update the field to NULL and after that we update to 1 and this overcomes the collection problem.

﹎☆浅夏丿初晴 2024-08-06 14:10:13

在 Bagisto 中创建类别时出现非法的排序规则混合。 运行这些命令(谢谢@Quy Le)为我解决了这个问题:

--set utf8 for connection

SET collation_connection = 'utf8_general_ci'

--change CHARACTER SET of DB to utf8

ALTER DATABASE dbName CHARACTER SET utf8 COLLATE utf8_general_ci

--changecategory table

ALTER TABLE categories CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

ALTER TABLE category_translations CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

Was getting Illegal mix of collations while creating a category in Bagisto. Running these commands (thank you @Quy Le) solved the issue for me:

--set utf8 for connection

SET collation_connection = 'utf8_general_ci'

--change CHARACTER SET of DB to utf8

ALTER DATABASE dbName CHARACTER SET utf8 COLLATE utf8_general_ci

--change category tables

ALTER TABLE categories CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

ALTER TABLE category_translations CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
月亮是我掰弯的 2024-08-06 14:10:13

就我而言,这很奇怪。 我从文件中读取 api 密钥,然后将其发送到进行 SQL 查询的服务器。 问题是 Windows 记事本留下的 BOM 字符,它导致错误如下:

SQLSTATE[HY000]: General error: 1267 Illegal mix of collat​​ions (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE )对于操作'='

我刚刚将其删除,一切都像魅力一样工作

In my case it was something strange. I read an api key from a file and then I send it to the server where a SQL query is made. The problem was the BOM character that the Windows notepad left, it was causing the error that says:

SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

I just removed it and everything worked like a charm

征﹌骨岁月お 2024-08-06 14:10:13

您需要为每个函数中的所有参数设置“utf8”。 这是我的情况:

在此处输入图像描述

You need to set 'utf8' for all parameters in each Function. It's my case:

enter image description here

抽个烟儿 2024-08-06 14:10:13
SELECT  username, AVG(rating) as TheAverage, COUNT(*) as TheCount
FROM    ratings
        WHERE month='Aug'
        AND username COLLATE latin1_general_ci IN
        (
        SELECT  username
        FROM    users
        WHERE   gender = 1
        )
GROUP BY
        username
HAVING
        TheCount > 4
ORDER BY
        TheAverage DESC, TheCount DESC;
SELECT  username, AVG(rating) as TheAverage, COUNT(*) as TheCount
FROM    ratings
        WHERE month='Aug'
        AND username COLLATE latin1_general_ci IN
        (
        SELECT  username
        FROM    users
        WHERE   gender = 1
        )
GROUP BY
        username
HAVING
        TheCount > 4
ORDER BY
        TheAverage DESC, TheCount DESC;
守护在此方 2024-08-06 14:10:13

如果您想避免更改语法来解决此问题,请尝试以下操作:

将 MySQL 更新到版本 5.5 或更高版本。

这为我解决了问题。

If you want to avoid changing syntax to solve this problem, try this:

Update your MySQL to version 5.5 or greater.

This resolved the problem for me.

爱你不解释 2024-08-06 14:10:13

确保您的 MySQL 版本支持子查询 (4.1+)。 接下来,您可以尝试将查询重写为如下所示:

SELECT ratings.username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM ratings, users 
WHERE ratings.month='Aug' and ratings.username = users.username
AND users.gender = 1
GROUP BY ratings.username
HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC

Make sure your version of MySQL supports subqueries (4.1+). Next, you could try rewriting your query to something like this:

SELECT ratings.username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM ratings, users 
WHERE ratings.month='Aug' and ratings.username = users.username
AND users.gender = 1
GROUP BY ratings.username
HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文