简体   繁体   中英

#1271 - Illegal mix of collations for operation 'UNION'

SELECT tbl_town.area_id, 
       tbl_town.area_name    
FROM tbl_town    
WHERE city_id = 1    

UNION 

SELECT CONCAT( tbl_town.area_id, '-', tbl_town_phase.town_phase_id ), 
       CONCAT( tbl_town.area_name, '-', tbl_town_phase.town_phase_name )    
FROM tbl_town, tbl_town_phase    
WHERE tbl_town.area_id = tbl_town_phase.town_id    
AND tbl_town.city_id = 1    

UNION 

SELECT CONCAT( tbl_town.area_id, '-', tbl_town_phase.town_phase_id, '-', tbl_town_block.town_block_id ), 
       CONCAT( tbl_town.area_name, '-', tbl_town_phase.town_phase_name, '-', tbl_town_block.town_block_name )    
FROM tbl_town, tbl_town_phase, tbl_town_block    
WHERE tbl_town.area_id = tbl_town_phase.town_id    
AND tbl_town_phase.town_phase_id = tbl_town_block.town_phase_id    
AND tbl_town.city_id =1    
LIMIT 0 , 30    

MySQL said: Documentation

#1271 - Illegal mix of collations for operation 'UNION

I have had a similar problem in the past. It's caused by mix of collations, most likely cause by differing column collations or default collation for strings.

This is what you you can try

  • check that the collation is the same for all columns
  • check that the collation for your database is the same as your columns
  • cast all your columns to the same collation as your database and run your script - then remove the collations in your query to find which column is causing the problem

Otherwise you query looks good.

Some small feedback on format would be to use joins for readability. Putting all your tables in your 'from' sections and then linking everything in the where clause can get confusing.

the error clearly said it.

you have to make same collations in your columns . if you use union UNION then make sure collation is same between tables and columns.

DROP PROCEDURE IF EXISTS `spConvertColumnCollation2utf8persianci`;
DELIMITER ;;
CREATE PROCEDURE spConvertColumnCollation2utf8persianci(DataBaseName VARCHAR(1000) CHARSET utf8)
BEGIN
    DECLARE Finished INT DEFAULT FALSE;
    DECLARE CN VARCHAR(500);
    DECLARE TN VARCHAR(500);
    DECLARE CL VARCHAR(3);
    DECLARE DT VARCHAR(100);
    DECLARE GetInvalidColumn CURSOR FOR SELECT c.COLUMN_NAME, c.TABLE_NAME, c.CHARACTER_MAXIMUM_LENGTH, c.DATA_TYPE FROM information_schema.COLUMNS c
                                        INNER JOIN information_schema.`TABLES` t ON (c.table_name = t.table_name)
                                        WHERE c.TABLE_SCHEMA = DataBaseName AND t.TABLE_TYPE <> 'VIEW' AND c.DATA_TYPE IN ('varchar', 'text', 'longtext') AND c.COLLATION_NAME NOT IN ('utf8_persian_ci');
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET Finished = TRUE;
    OPEN GetInvalidColumn;
    GetInvalidColumn_Loop: LOOP
        FETCH GetInvalidColumn INTO CN, TN, CL, DT;
    SET @qry = '';
    IF Finished THEN LEAVE GetInvalidColumn_Loop;
    ELSE
      SET @qry = CONCAT('ALTER TABLE ', TN, ' MODIFY COLUMN `', CN, '` ', DT,  IF(DT NOT IN ('text', 'longtext'), CONCAT(' (', CL ,') '), ''), ' CHARACTER SET utf8 COLLATE utf8_persian_ci;');
      PREPARE stmt1 FROM @qry;
      EXECUTE stmt1;  
    END IF;
    END LOOP GetInvalidColumn_Loop;
    CLOSE GetInvalidColumn;
END
;;
DELIMITER ;
  • change this method with your needed collation name

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