'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!
Solution 1:[1]
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.
Solution 2:[2]
To avoid "Illegal mix of collations for operation 'UNION'" MySQL error you must verify that same-index columns have the same type of collation or encoding.
Solution 3:[3]
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.
Solution 4:[4]
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
Solution 5:[5]
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
Note:The union operation requires that each of your two queries have exactly the same number of columns in their result set.
Solution 6:[6]
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;
Solution 7:[7]
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nikola Loncar |
Solution 2 | informatik01 |
Solution 3 | staticsan |
Solution 4 | |
Solution 5 | |
Solution 6 | |
Solution 7 | YugoAmaryl |