简体   繁体   中英

Error Code: 1271. Illegal mix of collations for operation 'UNION' on mysql

I have a view that is designed like this,

 SELECT ('header1,header2,header3,header' ) as `line` 
    UNION ALL 
 SELECT DISTINCT CONCAT(column1,column2,column3,column4) as `line`
from mytables t 
  join table2 gc ON (gc.code = t.code)
where t.myfield = getId() 

The getId() function looks like this,

DELIMITER $$
CREATE FUNCTION `getId`() RETURNS varchar(30) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
    NO SQL
    DETERMINISTIC
return @bid$$
DELIMITER ;

In order to trace the error I went through a process of elimination where I recreated the view by dropping and adding the columns and the error happens when column4 is added to the view.

The table definition for table2 where column4 is being taken from is,

CREATE TABLE `table2` (
  `code` int NOT NULL,
  `column4` varchar(100) NOT NULL
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I am using the following query on the view theview (which is where illegal mix of collations for operation 'UNION' error appears),

SELECT distinct s.* FROM (SELECT @bid:='AB') parm, `theview` s;

I am using the getId() function on other views and I am not getting the error there.

As per suggestion received to the question I added collate utf8mb4_unicode_ci to the first query, so the query now looks like,

SELECT ('header1,header2,header3,header' ) collate utf8mb4_unicode_ci as `line`

That looks like it has done the trick.

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