mysql union语句报错:Error Code: 1271. Illegal mix of collations for operation ‘UNION‘

在MySQL中使用UNION合并查询时遇到1271错误,原因是字符集不匹配。博客作者通过检查SQL语句发现涉及不同表的字符集问题。尝试更改表和字段的字符集未解决问题,因为现有记录的字符集未变。通过调整字段长度迫使数据重写,最终解决了问题。作者提到,更安全的方法可能是将数据移至临时表再重新插入,但考虑到自增字段的处理复杂性,选择了调整字段长度。
摘要由CSDN通过智能技术生成

字符集不同报错,使用union汇总结果集的两个表,字符集需要一致。如果表中已有记录,那么调整字符集,现有记录不会产生效果,这时需要重新插入记录,或改变字段长度,迫使内容重写。

最近在mysql的sql中使用union,总是报错,提示

Error Code: 1271. Illegal mix of collations for operation ‘UNION‘

从字面意思看,说什么非法混合,没说什么原因。我考察了一下我那条SQL语句,是从不同的表中读取,然后将结果用“union all”进行合并。字段有数值型也有字符型。如果将字符型全部去掉,可正常执行,否则出错。

        select * from (
              select
                  a.userid as id, a.userid as user_id,a.loginname as login_name, 1 as type, a.unitid as owner_id, 1 as status ,b.unitname as owner_name
              from org_user a inner join org_unit b on a.unitid=b.unitid and a.userstatus=0
              union all
              select
                  a.id, a.user_id,a.login_name, a.type, a.owner_id, a.status,b.name as owner_name
              from work_user a inner join work_club b on a.owner_id=b.id and a.type=0 and a.status=1
              union all
              select
                  a.id, a.user_id, a.login_name,a.type, a.owner_id, a.status,b.club_name as owner_name
              from work_user a inner join work_register_apply b on a.login_name=b.login_name and a.type=0 and a.status=0
          ) u where user_id=1000 limit 1

猜测应该是字符集的问题。mysql比较奇怪,每个表可以单独指定字符集(也许是每个字段都能单独指定字符集?试了一下,还真是)。
在这里插入图片描述
于是修改相应的字符集

ALTER TABLE `beian`.`tmp_work_club` 
CHANGE COLUMN `NAME` `NAME` VARCHAR(120) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_0900_as_ci' NOT NULL COMMENT '俱乐部名称' ;    

结果还是报错。真难伺候。世人爱mysql爱得那么狂热,没理由。

想起被调整的表已存在记录,估计是表字符集虽然变了,但现有的值的字符集并没有修改。于是将字段的长度调了一下,目的是迫使重写表数据。最后问题解决。

调整字段长度可能不是一个好的方法。也许将表移到临时表,然后再重新插入更好一些。但自增的字段比较麻烦。

在进行SQL注入时,你遇到了报错信息:"Illegal mix of collations for operation 'UNION'"。根据引用中的解决办法,这个问题是因为information_schema数据库中的表使用的字符集(collation)与目标网站user表中的字符集不一致导致的。解决方法是将user表中的字符集与information_schema数据库一致。需要注意的是,不仅仅需要更改表的字符集,还需要逐一更改每一个字段的字符集。 在实际实战中,如果无法更改目标网站的数据库,可以使用16进制的方式读取数据。根据引用中的实际测试,可以使用类似的payload和url来进行查询,并使用在线加解码工具解码返回结果。 如果你在修改字符集后仍然遇到相同的报错信息,可以参考引用中的方法。该方法使用SQL查询语句来生成修改表字段字符集的语句,可以批量修改表的字段字符集。你需要根据你自己的实际情况修改查询语句中的数据库名和字符集信息。 综上所述,你可以根据引用中提供的方法来解决报错信息"error:Illegal mix of collations for operation 'UNION'"。123 #### 引用[.reference_title] - *1* [SQL注入报错Illegal mix of collations for operationUNION‘ (sqli-labs靶场MySQL数据库)](https://blog.csdn.net/m0_47470899/article/details/118695774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* [关于SQL注入报错Illegal mix of collations for operationUNION‘原因剖析与验证](https://blog.csdn.net/qq_43665434/article/details/114088565)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *3* [MySQLSQL error1271Illegal mix of collations for operation UNION](https://blog.csdn.net/yyj108317/article/details/106427128)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值