简体   繁体   中英

MySQL error: Illegal mix of collations for operation 'UNION'

I am Boxonix!

I am currently making a comment section for my website, and I'm tiny a bit in trouble: This "Illegal mix of collations for operation 'UNION'" pops out when i run this query:

SELECT *
FROM comments
JOIN users ON comments.user_id = users.id
ORDER BY users.id DESC LIMIT $LIMIT1
UNION
SELECT *
FROM comments
JOIN videos ON comments.video_id = videos.id

I am already kind a bit confused, I'm not using MySQL that often!
Please help!

You can force collation in SELECT part of query. For example if column videos.comment is utf8_general_ci and comments.comment is utf8_unicode_ci you can force it to be same:

SELECT 
   comment COLLATE utf8_general_ci
FROM comments
UNION
SELECT 
   comment
FROM videos

But this has limited use in case that collations can't be translated exactly as you want.

为了避免“操作'UNION'的排序规则的非法混合”Mysql错误,您必须验证相同索引列具有相同类型的排序规则或编码。

The problem is that the two sub-queries will have different column sets.

The first sub-query is going to give you all columns from comments and all columns from users . The second sub-query is going to give you all columns from comments and all columns from videos . Unless the two tables users and videos have the exact same column definitions, then the UNION is not going to work.

You need to decide what columns you need from each query and then customise each SELECT ... so that they match.

You can't use order by statement before union, If you want use first you have to convert the result as temporary table. then say order by condition as below

select * from (SELECT * FROM comments
JOIN users ON comments.user_id = users.id
UNION
SELECT * FROM comments
JOIN videos ON comments.video_id = videos.id) as t order by id DESC LIMIT 1

the important note here is that both users and videos should have same number of column as comments table..Because if you use union or union all you should take equal no. of columns with equal data type..

If comments,users,videos has different number of columns then don't user select * from. instead select all columns explicity as follow

If comments table has columns user_id,col1,col2,col3 then

select user_id,col1,col2,col3 from (SELECT c.user_id,col1,col2,col3 FROM 
comments c JOIN users u c.user_id = u.id
UNION
SELECT c.user_id,col1,col2,col3 FROM comments c
JOIN videos v ON c.video_id = v.id) as t 
order by user_id DESC LIMIT 1

Replace $LIMIT1 with 1 and perform ORDER BY after second query.Try this:

(SELECT *
FROM comments
JOIN users ON comments.user_id = users.id)
UNION
(SELECT *
FROM comments
JOIN videos ON comments.video_id = videos.id)
ORDER BY id DESC LIMIT 1

The union operation requires that each of your two queries have exactly the same number of columns in their result set.联合操作要求您的两个查询中的每一个在其结果集中具有完全相同的列数。

Simply you have to compare the all the table collation column and it should be same in all the table Like if there is utf8_general_ci then all the tables which you want to merge with union should be having utf8_general_ci or if different then change it from structure or use following query.

ALTER TABLE 
`Tablename` 
CHANGE 
`columnname` 
`columnname` VARCHAR(191) 
CHARACTER SET 
utf8 
COLLATE 
utf8_general_ci 
NULL 
DEFAULT NULL;

I encountered this error recently. The columns in the union must have the same collations

Check the collations of the columns.

show full columns from <table_name>;

and change the collations on the column.

alter table <table_name> modify <column_name> varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci default null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM