赞
踩
Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation ‘=’
子查询时, 两张表的字符集编码不同,需要在字段后面 加上COLLATE utf8mb4_unicode_ci
报的错误为:
1267 - Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation ‘=’
解决方案:
在子查询的字段后面加上 COLLATE utf8mb4_unicode_ci
表连接查询时, 两张表的字符集编码不同,需要在表连接on的后面 加上COLLATE utf8mb4_unicode_ci
SELECT sta.user_id AS userId, ifnull( a.customerCount, 0 ) AS customerCount, ifnull( ( ifnull( enterGroup.enterGroupChatCount, 0 ) / ifnull( a.customerCount, 0 ) ), 0.0000 ) AS customerIncomingGroupPercent, ifnull( enterGroup.enterGroupChatCount, 0 ) AS enterGroupChatCount FROM cpwx_member_customer_statistic sta LEFT JOIN ( SELECT count( DISTINCT external_userid ) AS customerCount, user_id AS userId FROM cpwx_customer_employee_rel WHERE deleted = 0 AND clique_id = "22313852750823424" AND add_time <= "2020-11-10 23:59:59" GROUP BY user_id ) a ON sta.user_id = a.userId LEFT JOIN ( SELECT count( DISTINCT chatUser.userid ) AS enterGroupChatCount, chat.`owner` AS userId FROM -- 这里表连接的两张表字符集相同,都是COLLATE = utf8mb4_unicode_ci ,所以不需要加 COLLATE utf8mb4_unicode_ci cpwx_groupchat_user chatUser LEFT JOIN cpwx_groupchat chat ON chatUser.chat_id = chat.chat_id WHERE chatUser.deleted = 0 AND chatUser.type = 2 AND chatUser.clique_id = "22313852750823424" AND chatUser.join_time <= "2020-11-10 23:59:59" AND chat.clique_id = "22313852750823424" GROUP BY chat.`owner` ) enterGroup ON enterGroup.userId = sta.user_id -- 这里表连接的两张表字符集不同,需要加上COLLATE utf8mb4_unicode_ci COLLATE utf8mb4_unicode_ci WHERE sta.deleted = 0 AND sta.clique_id = "22313852750823424" AND sta.statistic_date >= "2020-11-09 00:00:00" AND sta.statistic_date <= "2020-11-09 23:59:59" ORDER BY customerIncomingGroupPercent DESC, customerCount DESC, userId DESC
其中 cpwx_groupchat_user 和 cpwx_groupchat 表的字符集为utf8mb4_unicode_ci
cpwx_member_customer_statistic 和 cpwx_customer_employee_rel 字符集为utf8mb4_general_ci
字符集不同时,区分子查询还是表连接, 加载字段后面还是表连接后面,也是一点点试出来的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。