53

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

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,IMPLICIT) 和 (latin1_general_ci,IMPLICIT) 用于操作 '='

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

4

20 回答 20

101

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

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

于 2011-04-21T16:32:04.360 回答
22

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

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

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

于 2009-09-29T10:01:24.807 回答
19

[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.

于 2016-05-17T11:58:34.537 回答
7

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.

于 2012-08-21T21:23:39.123 回答
4

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
于 2016-03-03T06:53:20.013 回答
2

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);

于 2018-11-29T11:29:48.493 回答
1
  • 检查您的 users.gender 列是否为 INTEGER。
  • 尝试:alter table users convert to character set latin1 collate latin1_swedish_ci;
于 2009-08-06T22:31:51.197 回答
1

You need to change each column Collation from latin1_general_ci to latin1_swedish_ci

于 2012-07-25T17:50:31.247 回答
1

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.

于 2014-01-23T13:49:01.057 回答
1

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!

于 2014-03-26T11:34:06.933 回答
1

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

于 2016-07-07T07:01:12.647 回答
0

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

于 2016-08-24T11:46:00.153 回答
0

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.

于 2016-09-13T13:49:16.393 回答
0

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.

于 2017-04-07T12:37:51.617 回答
0

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

enter image description here

于 2019-02-03T18:58:26.527 回答
0

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
于 2020-11-28T16:29:13.833 回答
0

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
于 2021-01-15T08:57:17.750 回答
0

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

于 2021-08-17T20:26:51.303 回答
-1
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;
于 2009-08-06T22:48:01.587 回答
-2

确保您的 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
于 2009-08-06T22:27:49.863 回答