mysql使用union all时字符集不一致
mysql 使用union all时碰到的一个奇怪现象:
问题描述
写了一个稍微有一点复杂的sql:先根据where条件查询出结果集,然后union all结果集中的各个列的sum。简单来说,就是在结果的最后一行加上一个总和。只不过这一步是放在sql中执行,而不是程序
然后返回让两个union all 前后的内容交换就报错:
Illegal mix of collations (utf8mb4_0900_ai_ci,COERCIBLE) and (gbk_chinese_ci,IMPLICIT) for operation
原来能正确执行的sql:
SELECT gbk_office as office,leader_name ,leader_id as leaderId,count(DISTINCT (opp.id)) as sjzs,count(DISTINCT (case when foll.id is null then null else opp.id end)) as ygjsj,count(DISTINCT (foll.creator_id)) as gjrs,COUNT(foll.id) as gjjls,max(opp.last_follow_date) as zhgjsj,COUNT(DISTINCT (case when foll.type = 1 then opp.id else null end)) as smbfs,COUNT(DISTINCT (case when opp.stage_id = 2 then opp.id else null end)) as xsjd,COUNT(DISTINCT (case when opp.stage_id = 3 then opp.id else null end)) as yxjd,COUNT(DISTINCT (case when opp.stage_id = 4 then opp.id else null end)) as bfjd,COUNT(DISTINCT (case when opp.stage_id = 5 then opp.id else null end)) as swjd,COUNT(DISTINCT (case when opp.stage_id = 6 then opp.id else null end)) as wcqysfrom channel_partner_opportunity oppLEFT JOIN channel_partner_opportunity_follow follon opp.id = foll.cpo_idGROUP BY gbk_office, leader_name
union allselect '合计' as office,null as leader_name,null as leaderId,sum(sjzs) as sjzs,sum(ygjsj) as ygjsj,sum(gjrs) as gjrs,sum(gjjls) as gjjls,max(zhgjsj) as zhgjsj,sum(smbfs) as smbfs,sum(xsjd) as xsjd,sum(yxjd) as yxjd,sum(bfjd) as bfjd,sum(swjd) as swjd,sum(wcqys) as wcqysfrom (SELECT gbk_office as office,leader_name,leader_id as leaderId,count(DISTINCT (opp.id)) as sjzs,count(DISTINCT (case when foll.id is null then null else opp.id end)) as ygjsj,count(DISTINCT (foll.creator_id)) as gjrs,COUNT(foll.id) as gjjls,max(opp.last_follow_date) as zhgjsj,COUNT(DISTINCT (case when foll.type = 1 then opp.id else null end)) as smbfs,COUNT(DISTINCT (case when opp.stage_id = 2 then opp.id else null end)) as xsjd,COUNT(DISTINCT (case when opp.stage_id = 3 then opp.id else null end)) as yxjd,COUNT(DISTINCT (case when opp.stage_id = 4 then opp.id else null end)) as bfjd,COUNT(DISTINCT (case when opp.stage_id = 5 then opp.id else null end)) as swjd,COUNT(DISTINCT (case when opp.stage_id = 6 then opp.id else null end)) as wcqysfrom channel_partner_opportunity oppLEFT JOIN channel_partner_opportunity_follow follon opp.id = foll.cpo_idGROUP BY gbk_office, leader_name) tableA
修改后的sql(仅交换了union all 前后的内容位置):
select '合计' as office,null as leader_name,null as leaderId,sum(sjzs) as sjzs,sum(ygjsj) as ygjsj,sum(gjrs) as gjrs,sum(gjjls) as gjjls,max(zhgjsj) as zhgjsj,sum(smbfs) as smbfs,sum(xsjd) as xsjd,sum(yxjd) as yxjd,sum(bfjd) as bfjd,sum(swjd) as swjd,sum(wcqys) as wcqysfrom (SELECT gbk_office as office,leader_name,leader_id as leaderId,count(DISTINCT (opp.id)) as sjzs,count(DISTINCT (case when foll.id is null then null else opp.id end)) as ygjsj,count(DISTINCT (foll.creator_id)) as gjrs,COUNT(foll.id) as gjjls,max(opp.last_follow_date) as zhgjsj,COUNT(DISTINCT (case when foll.type = 1 then opp.id else null end)) as smbfs,COUNT(DISTINCT (case when opp.stage_id = 2 then opp.id else null end)) as xsjd,COUNT(DISTINCT (case when opp.stage_id = 3 then opp.id else null end)) as yxjd,COUNT(DISTINCT (case when opp.stage_id = 4 then opp.id else null end)) as bfjd,COUNT(DISTINCT (case when opp.stage_id = 5 then opp.id else null end)) as swjd,COUNT(DISTINCT (case when opp.stage_id = 6 then opp.id else null end)) as wcqysfrom channel_partner_opportunity oppLEFT JOIN channel_partner_opportunity_follow follon opp.id = foll.cpo_idGROUP BY gbk_office, leader_name) tableAunion allSELECT gbk_office as office,leader_name ,leader_id as leaderId,count(DISTINCT (opp.id)) as sjzs,count(DISTINCT (case when foll.id is null then null else opp.id end)) as ygjsj,count(DISTINCT (foll.creator_id)) as gjrs,COUNT(foll.id) as gjjls,max(opp.last_follow_date) as zhgjsj,COUNT(DISTINCT (case when foll.type = 1 then opp.id else null end)) as smbfs,COUNT(DISTINCT (case when opp.stage_id = 2 then opp.id else null end)) as xsjd,COUNT(DISTINCT (case when opp.stage_id = 3 then opp.id else null end)) as yxjd,COUNT(DISTINCT (case when opp.stage_id = 4 then opp.id else null end)) as bfjd,COUNT(DISTINCT (case when opp.stage_id = 5 then opp.id else null end)) as swjd,COUNT(DISTINCT (case when opp.stage_id = 6 then opp.id else null end)) as wcqysfrom channel_partner_opportunity oppLEFT JOIN channel_partner_opportunity_follow follon opp.id = foll.cpo_idGROUP BY gbk_office, leader_name;
报错信息:
Illegal mix of collations (utf8mb4_0900_ai_ci,COERCIBLE) and (gbk_chinese_ci,IMPLICIT) for operation
原因分析:
有某列的字符集不一致导致
执行sql1:show full columns from 表名;
查出当前表的全部列信息
执行结果:
其中collation是字符集列。在出问题前,有一列的coolation不是utf8_general_ci,而是gbk_chinese_ci
执行sql2:SHOW COLLATION;
执行结果:
可以在其中搜到,发生问题的:gbk_chinese_ci 字符集是gbk。而其他列的:utf8_general_ci字符集utf8。两个是不一样的
因此,得出结论:是 gbk_offic 列的字符集与其他列的不同,导致的错误。
解决方案:
修改与其他列字符集不同的列,与其他列保持一致
新增:alter table 表名 add column 列名 varchar (50) character set utf8;
修改:alter table 表名 modify column 列名 varchar(50) character set utf8;
小tip:
因为是解决了问题后,才来记录的这个bug,所以可能有一些信息截图中与保存信息不一致的存在。
- 报错信息是不知道修改调试到哪一个版本进行的。所以可能和解决方案中描述的不太一样,但是不影响解决思路,
- 解决方案中,我先用了修改,然后报错:‘Changing the STORED status’ is not supported for generated columns. 所以我直接使用了新增,然后删除了这个旧列,然后重命名新列。这样一个方法,因为时间有限
- 之所以换位置就会报错。是因为老版本中,将字符集不同的gbk_office当做输出结果,新版本中:将null 作为 offIce的结果,去关联字符集不同的列。故而报错
mysql使用union all时字符集不一致相关推荐
- mysql 字符集 性能_MySQL字符集不一致导致性能下降25%,你敢信?
故事是这样的: 我在对MySQL进行性能测试时,发现CPU使用率接近100%,其中80%us, 16%sys,3%wa,iostat发现磁盘iops2000以下,avgqu-sz不超过3,%util最 ...
- MySQL连接字符串,可以自定义连接时字符集编码
MySQL连接字符串,可以自定义连接时字符集编码 jdbc:mysql://localhost:3306/${database}?useUnicode=true&characterEncodi ...
- MySQL 字符集不一致导致索引失效的一个真实案例
文章目录 问题描述 问题分析 总结 大家好,我是只谈技术不剪发的 Tony 老师.今天给大家分析一个由于 MySQL 字符集不一致导致索引失效的案例. 问题描述 有个朋友给我发来一个问题,说是他们的系 ...
- oracle imp导入修改字符集,Oracle中IMP导入数据时提示字符集不一致解决
生产环境中经常使用到Oracle的IMP导入和EXP导出来功能来达到数据迁移的目的,通常在源数据库和目标数据库中查询字符集是否致, 测试环境中导入IMP导入报错信息如下: 导入命令如下: [oracl ...
- MySQL高级篇01【字符集、SQL规范和sql_mode设置】
目录 1. 字符集操作 1.1 修改MySQL5.7字符集 1. 修改步骤 2.已有库/表字符集的变更 1.2 字符集级别分类 1. 服务器级别 2. 数据库级别 3. 表级别 4. 列级别 5. 小 ...
- mysql提示符详解_MySQL字符集使用详解
查看字符集相关变量 mysql> show variables like 'character%'; +--------–+-----------+ | Variable_name | Valu ...
- unicode表_Python数据库操作 Mysql数据库表引擎与字符集#学习猿地
# Mysql数据库表引擎与字符集 ![](./imgs/752951346A5F4E7EBDE362FA97107707.png) ### 1.服务器处理客户端请求 其实不论客户端进程和服务器进程是 ...
- mysql报错乱码_连接mysql服务器报错时,出现乱码
页头用了 header('content-type:text/html;charset=utf-8'); try { $this->dbo=new PDO($dsn,$dbuser,$dbpas ...
- 连接mysql报错有乱码_连接mysql服务器报错时,出现乱码
页头用了 header('content-type:text/html;charset=utf-8'); try { $this->dbo=new PDO($dsn,$dbuser,$dbpas ...
最新文章
- java ip包_java网络抓ip包 首部是个什么情况
- SAP MM Error message - Customizing incorrectly maintained – in transaction code ML81N
- iOS 新窗口在最上层
- 纠错--跟我学Java第44页
- 2019 Multi-University Training Contest 2 - 1008 - Harmonious Army - 最大流
- .Net下的HashTable
- jvm性能调优实战 -58类加载器过多引发的OOM问题
- hdu 1024(滚动数组优化)
- C++ pair(对组)用法
- python全栈开发_day10_函数的实参和形参
- css flexbox模型_如何将Flexbox后备添加到CSS网格
- css实现风车转动,纯CSS实现的风车转动效果特效演示
- 【转】脉络清晰的BP神经网络讲解,赞
- 在FireFox中使用IE Tab插件
- 使用QT:复刻俄罗斯方块游戏
- selenium web自动化判断页面元素加载完毕
- android手机屏幕总是闪烁,手机屏幕闪烁是什么原因
- 京东客服岗位人才考试
- 当display:flex弹性布局与position:absolute/fixed定位一起用,会出现的问题与解决方法
- java导出excel
热门文章
- 2022面试200题目和答案分布式+微服务+MYSQL+Redis+JVM+Spring
- 国考“同意报考证明”难倒考生
- 小吴学汇编之第一章基础知识 (2)
- Android-App性能测试工具GT的使用方法
- 安装BENQSCANNER 5560驱动与测试扫描
- 南卡跟华为降噪蓝牙耳机哪款好用?南卡跟华为降噪耳机测评
- 强化学习在资源优化领域的应用
- php如何生成一年的日历表_html-PHP如何生成一个指定年份一整年的日历
- 在线jpg图片格式转换器
- 发生系统错误 1275.此驱动程序被阻止加载 解决方案