【MySQL】java.sql.SQLException: Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation '='
问题原因参考:
http://t.zoukankan.com/zhulei2-p-13451554.html
collations 排序规则
Illegal mix 非法混合
SQL报错指出,操作符等号,非法混合了不同的字符集
我先看看库字符集设定:
然后再看看两张表的:
CREATE TABLE `tm_company` ( `COMPANY_ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'DEALER_ID', `APP_ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `COMPANY_CODE` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '公司代码', `COMPANY_TYPE` int(8) NOT NULL COMMENT '公司类型:1506,和org表org_type相同', `COMPANY_NAME_CN` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司中文全称', `COMPANY_NAME_EN` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司英文全称', `COMPANY_SHORT_NAME_CN` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司简称中文', `COMPANY_SHORT_NAME_EN` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司简称英文', `PROVINCE_ID` int(11) DEFAULT NULL COMMENT '省份', `PROVINCE_NAME` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '省份', `CITY_ID` int(11) DEFAULT NULL COMMENT '城市', `CITY_NAME` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '城市', `COUNTY_ID` int(11) DEFAULT NULL COMMENT '所在区县', `COUNTY_NAME` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '所在区县', `ADDRESS_CN` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司地址中文', `ADDRESS_EN` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司地址英文', `COMPANY_KIND` int(8) DEFAULT NULL COMMENT '经销商类型2538:4S店、单一销售、单一售后、直营店、非直营、大客户', `USED_COMPANY_CODE` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '曾用经销商code', `TRANSFER_COMPANY_CODE` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '转为经销商code', `ORG_ID` bigint(20) DEFAULT NULL COMMENT '组织ID,与组织表对应', `GROUP_ORG_ID` bigint(20) DEFAULT NULL COMMENT '所属集团的组织ID', `VALID_STATUS` int(8) DEFAULT NULL COMMENT '有效状态:1001', `DATA_TYPE` int(8) DEFAULT NULL COMMENT '数据类型1046:经销商、集团、车厂', `COMPANY_PROPERTITY` int(8) DEFAULT NULL COMMENT '经销商属性(普通/车厂虚拟/区域虚拟)', `LINK_MAN` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '联系人', `PHONE` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '联系电话', `ZIP_CODE` varchar(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '邮编', `FAX` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '传真', `E_MAIL` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'E_MAIL', `LATITUDE` double(28,20) DEFAULT NULL COMMENT '经度', `LONGITUDE` double(28,20) DEFAULT NULL COMMENT '维度', `COMPANY_HOMEPAGE` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司主页', `OPEN_DATE` date DEFAULT NULL COMMENT '开业日期', `CLOSE_DATE` date DEFAULT NULL COMMENT '停业日期', `CREATED_DATE` date DEFAULT NULL COMMENT '建站日期', `IS_USED_CAR` tinyint(4) DEFAULT NULL COMMENT '是否经营二手车1004', `STATUS` int(8) DEFAULT NULL COMMENT ' 业务显示:待开业,已开业,停业', `REMARK` varchar(400) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注', `DEALER_COUNT` int(4) DEFAULT NULL COMMENT '经销商店数量', `IS_MAIN` int(8) DEFAULT NULL COMMENT '是否主店:多家经销商渠道的时候区分哪个主店', `BUSINESS_BEGIN_HOURS` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '营业开始时间', `BUSINESS_END_HOURS` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '营业结束时间', `CHANNEL_PICTURE` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '经销商图片', `USER_ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '管理员ID', `OLD_DEALER_CODE` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'CDK DMS 代码', `RECORD_VERSION` int(11) NOT NULL DEFAULT '0' COMMENT 'RECORD_VERSION', `CREATED_BY` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '-1' COMMENT 'CREATED_BY', `CREATED_AT` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'CREATED_AT', `UPDATED_BY` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '-1' COMMENT 'UPDATED_BY', `UPDATED_AT` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'UPDATED_AT', `IS_DELETED` tinyint(4) NOT NULL DEFAULT '0', `IS_SATELLITE_STORE` int(8) DEFAULT '10041002' COMMENT '是否卫星店', `OWNED_COMPANY_NAME` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '所属经销商', PRIMARY KEY (`COMPANY_ID`) USING BTREE, UNIQUE KEY `AK_TM_COMPANY_10` (`APP_ID`,`COMPANY_CODE`) USING BTREE, KEY `IDX_TM_COMPANY_10` (`ORG_ID`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1273161411814670341 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='公司表,车厂、经销商公司、经销商集团等';
专属价格表:
CREATE TABLE `tm_part_own_price` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `OWNER_CODE` varchar(36) DEFAULT NULL COMMENT '所有者代码(经销商?)', `PART_NO` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '配件代码', `OWN_PRICE` decimal(10,2) DEFAULT NULL COMMENT '专属价格', `BEGIN_AT` datetime DEFAULT NULL COMMENT '开始时间', `END_AT` datetime DEFAULT NULL COMMENT '结束时间', `CREATE_TIME` datetime DEFAULT NULL COMMENT '创建时间', `CREATE_BY` bigint(20) DEFAULT NULL COMMENT '创建者代码', `UPDATE_TIME` datetime DEFAULT NULL COMMENT '更新时间', `UPDATE_BY` bigint(20) DEFAULT NULL COMMENT '更新者代码', `RECORD_VERSION` bigint(11) DEFAULT NULL COMMENT '乐观锁记录', PRIMARY KEY (`ID`), KEY `IDX_OWNER_CODE` (`OWNER_CODE`) USING BTREE COMMENT '所有者代码索引', KEY `IDX_PART_NO` (`PART_NO`) USING BTREE COMMENT '配件代码索引' ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='配件专属价格表';
专属表是我自己写的,默认走库的字符集,但是tm_company表是复制过来的,字符集和库和我的表不一致
要解决这个报错就是统一一致的字符集设定,鉴于库是900_ai_ci,应该是更改company表的字符集
但是随后发现还是报这个错误:
除了表级别的字符集,字段级别的也有设置字符集
连表的条件:
tm_part_own_price price JOIN tm_company coop ON price.OWNER_CODE = coop.COMPANY_CODE
查看链表的字段字符集:
`COMPANY_CODE` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '公司代码', `OWNER_CODE` varchar(36) DEFAULT NULL COMMENT '所有者代码(经销商?)',
OWNER_CODE没有设置,默认跟表字符集一起是900_ai_ci,而company表还是general_ci
再找到字段进行修改,这时终于正常了