数据库(Mysql)表与表字符集不统一带来的麻烦事儿

1、查看数据库字符集:

show variables like '%collation%';

2、查看表A、表B、表C的字符集:

show full columns from 表名;

show table status from 库名 like '表名';

表A的字符集是utf8mb4_general_ci、表B的字符集是utf8mb4_general_ci、表C的字符集是utf8mb4_unicode_ci、

3、带来的问题

表A和表B关联查询时应该字符集不统一返回失败:[HY000][1267] Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='

4、解决办法

  • 临时解决方案:在查询时指定字符集(不推荐,因为字符集不统一,table_c不能使用索引,如果数据量很大时,查询速度很慢
select a.file_key, c.biz_id, c.create_time, c.status
    from table_a t, table_b c ,table_c a
    where t.template_id = c.template_id
    and t.template_code = 't_001'
    and c.customer_id ='1234323454323432'
    and c.customer_id=a.customer_id collate utf8mb4_unicode_ci
    and a.biz_id collate utf8mb4_unicode_ci =c.biz_id
    order by c.create_time desc
    limit 1;
  • 永久解决,修改表字符集(推荐)
alter table 表名 convert to character set utf8mb4 collate utf8mb4_general_ci;

项目规范一定要建立,该统一的一定要统一。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燕少༒江湖

给我一份鼓励!谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值