#1271 - 操作'UNION'的非法混合排序

时间:2014-04-08 06:05:23

标签: mysql sql collation

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说:文档

  

#1271 - 非正式混合操作' UNION

3 个答案:

答案 0 :(得分:1)

过去我遇到过类似的问题。它是由排序组合引起的,很可能是由于字符串排序不同或字符串的默认排序规则造成的。

这是你可以试试的

  • 检查所有列的排序规则是否相同
  • 检查数据库的排序规则是否与列相同
  • 将所有列投射到与数据库相同的排序规则并运行脚本 - 然后删除查询中的排序规则以查找导致问题的列

否则您的查询看起来不错。

对格式的一些小反馈是使用连接以提高可读性。将所有表放在'from'部分中,然后将where子句中的所有内容链接起来会让人感到困惑。

答案 1 :(得分:0)

错误清楚地说出来了。

您必须在列中进行相同的排序规则。如果你使用union UNION,那么确保表和列之间的排序相同。

答案 2 :(得分:0)

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 ;
  • 使用所需的归类名称更改此方法