1. 程式人生 > 其它 >資料庫(Mysql)表與表字符集不統一帶來的麻煩事兒

資料庫(Mysql)表與表字符集不統一帶來的麻煩事兒

技術標籤:mysqlmysqlmysql字符集字符集修改檢視字符集資料庫

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;

專案規範一定要建立,該統一的一定要統一。