Illegal mix of collations for operation 'concat' while using union all

只愿长相守 提交于 2021-02-08 08:53:47

问题


First of all,English is not my mother language,if I made some mistakes,please forgive me. This problem occurs when I try to push my data from mysql5.7 to Redis,it is about MySQL operation concat.

Without the UNION ALL,and I split it into 2 SQLs,it works.But once I run it with UNION ALL', the error occurs.

[Err] 1271 - Illegal mix of collations for operation concat

SELECT CONCAT( 
    "*4\r\n",
    '$',LENGTH(redis_cmd),'\r\n',redis_cmd,'\r\n',
    '$',LENGTH(redis_key),'\r\n',redis_key,'\r\n',
    '$',LENGTH(redis_score),'\r\n',redis_score,'\r\n',
    '$',LENGTH(redis_val),'\r\n',redis_val,'\r'
) 
FROM (
SELECT
    'ZADD' AS redis_cmd,
    'GS_TM' AS redis_key,
    s1.QSH AS redis_score,
    CONCAT(s1.GS,':',s1.QSH,':0') AS redis_val
FROM
    gstmfw s1
UNION ALL
SELECT
    'ZADD' AS redis_cmd,
    'GS_TM' AS redis_key,
    s2.ZZH AS redis_score,
    CONCAT(s2.GS,':',s2.ZZH,':1') AS redis_val
FROM
    gstmfw s2
) AS gstmfw;

I want to do the whole job in one sql,but not a solution. In the end,thanks for reading and helping me.


回答1:


You have different collation for your tables/columns.

Example: latin1_swedish_ci and utf8_general_ci

This post could help: error Illegal mix of collations for concat
And this: Illegal mix of collations for operation 'concat'




回答2:


Check in your table's schema the collation you are reallyusing try convert the wrong collation (or both) in the same
eg usint utf8

  SELECT CONCAT( 
      "*4\r\n",
      '$',LENGTH(redis_cmd),'\r\n',redis_cmd,'\r\n',
      '$',LENGTH(redis_key),'\r\n',redis_key,'\r\n',
      '$',LENGTH(redis_score),'\r\n',redis_score,'\r\n',
      '$',LENGTH(redis_val),'\r\n',redis_val,'\r'
  ) 
  FROM (
  SELECT
      'ZADD' AS redis_cmd,
      'GS_TM' AS redis_key,
      s1.QSH AS redis_score,
      CONCAT(s1.GS,':',s1.QSH,':0') AS redis_val
  FROM
      gstmfw s1
  UNION ALL
  SELECT
      convert('ZADD' USING utf8) AS redis_cmd,
      convert('GS_TM'USING utf8) AS redis_key,
      convert(s2.ZZH USING utf8) AS redis_score,
      convert(CONCAT(s2.GS,':',s2.ZZH,':1') USING utf8) AS redis_val
  FROM
      gstmfw s2
  ) AS gstmfw;


来源:https://stackoverflow.com/questions/57568094/illegal-mix-of-collations-for-operation-concat-while-using-union-all

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!