SQLERROR:1267

本文介绍了一种在MySQL数据库中进行表间联接查询时遇到的字符集不匹配问题,具体表现为utf8_unicode_ci与utf8_general_ci两种字符集无法直接比较,导致查询失败。文章详细记录了解决该问题的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在使用MySQL5.x时,用到了两个表的组合查询,类似下面简单的语句: 
Java代码  收藏代码
  1. select pa.userId as userId,pa.userName as userName,pb.orgId as orgId,pb.orgName as orgName from tbl_user pa,tbl_org pb where pa.userId = pb.userId  

结果却出现了下面的错误: 
Java代码  收藏代码
  1. SQL Error: 1267, SQLState: HY000  
  2. [@APPNAME@] ERROR [http-8080-2] JDBCExceptionReporter.error(104) | Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='  
  3. org.hibernate.exception.GenericJDBCException: could not execute query using iterate at org.hibernate.exception.SQLStateConverter.handledNonSpecificException  
  4. (SQLStateConverter.java:103)  
  5.     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)  
  6.     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)  
  7.     at org.hibernate.loader.hql.QueryLoader.iterate(QueryLoader.java:427)  
  8.     at org.hibernate.hql.ast.QueryTranslatorImpl.iterate(QueryTranslatorImpl.java:380)  
  9. ... ...  

这个问题仅仅在自己家里的机器上的MySQL本地数据库中出现,在公司的服务器数据库上则不会出现。因为之前一直在用oracle数据库,MySQL相对接触较少,连续几天这个问题都没有解决,只是觉得这个问题太过莫名其妙了,百思不得其解。 
大概到了第3天吧,突然明白了错误提示的意思,这意思是在说utf8_general_ci和utf8_unicode_ci由于隐含的字符集不同,不能用'='进行判断。 
立即用客户端软件SQLyog连接到数据库,检查创建数据库表的语句,果然发现下面的小小区别 
Java代码  收藏代码
  1. CREATE TABLE `tbl_user` (  
  2.   `userid` bigint(20) NOT NULL AUTO_INCREMENT,  
  3.   `version` bigint(20) DEFAULT '0',  
  4. ... ...  
  5. ) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;  
  6.   
  7. CREATE TABLE `tbl_org` (  
  8.   `orgid` bigint(20) NOT NULL AUTO_INCREMENT,  
  9.   `version` bigint(20) DEFAULT '0',  
  10. ... ...  
  11. ) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;  

注意两条语句最后的COLLATE后面的那一点,一个是utf8_unicode_ci,而另一个是utf8_general_ci,这里描述的是数据库校对规则,由于两个表这里的差异,造成了上面的错误。 
删除其中的一个表,重新创建之,使二者数据库校对规则一致后,问题解决。 
2025-04-02 09:19:12.680 WARN 14624 --- [nio-5000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1267, SQLState: HY000 2025-04-02 09:19:12.680 ERROR 14624 --- [nio-5000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8mb4_unicode_ci,COERCIBLE) for operation '=' 2025-04-02 09:19:12.805 ERROR 14624 --- [nio-5000-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8mb4_unicode_ci,COERCIBLE) for operation '=' at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1019) ~[mysql-connector-java-8.0.11.jar:8.0.11] at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-3.4.2.jar:na] at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na] at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final] at org.hibernate.loader.Loader.getResultSet(Loader.java:2292) ~[hibernate-core-5.4.12.Final.ja
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值