栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Illegal mix of collations (XXX) and (XXX) for operation ‘XXX’

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Illegal mix of collations (XXX) and (XXX) for operation ‘XXX’

问题描述:

mybatis使用INSTR的时候报错(数据库为MySQL)—— Illegal mix of collations (XXX) and (XXX) for operation 'XXX’
– Cause: java.sql.SQLException: Illegal mix of collations (utf8_bin,NONE) and (utf8mb4_general_ci,COERCIBLE) for operation ‘instr’
– uncategorized SQLException; SQL state [HY000]; error code [1267]; Illegal mix of collations (utf8_bin,NONE) and (utf8mb4_general_ci,COERCIBLE) for operation ‘instr’; nested exception is java.sql.SQLException: Illegal mix of collations (utf8_bin,NONE) and (utf8mb4_general_ci,COERCIBLE) for operation ‘instr’


原因分析:

排序规则不同导致的


解决方案:

1、查询SQL语句中使用到的数据表和字段的排序规则

-- 查询数据表的排序规则
select table_name, table_collation
from information_schema.tables
where table_schema = database()
and table_name in ('表名1', '表名2');
-- 查询字段的排序规则
select table_name, column_name, collation_name
from information_schema.columns
where table_schema = database()
and collation_name is not null
and table_name in ('表名1', '表名2');

2、然后修改不一样的数据表的排序规则(如果能定位是哪两个表起冲突了的话就直接改对应表就好,不知道的话就只能全改了)

-- 修改排序规则
-- 这里和编码规则一起修改了,编码规则和排序规则好像还有对应关系
-- 排序规则(collate)必须以编码规则(set)开头
-- 修改数据表,表中的字段也会一起被修改
ALTER TABLE 表名 ConVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
-- 修改某个字段
ALTER TABLE 表名 MODIFY 字段名 VARCHAr(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

注意:

那个查询表的排序规则的时候,排序规则要和其他大多数数据表的排序规则保持一致,要不然其他功能会出问题。

-- 查询数据库中全部数据表的排序规则
select table_name, table_collation
from information_schema.tables
where table_schema = database();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号