MySQL多表join時(shí)報(bào)錯(cuò)如下:[Err]1267 – Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=
就是說(shuō)兩個(gè)表的排序規(guī)則(COLLATION)不同,無(wú)法完成比較。COLLATION是用在排序,大小比較上,一個(gè)字符集有一個(gè)或多種COLLATION,并且以_ci(大小寫(xiě)不敏感)、_cs(大小寫(xiě)敏感)或_bin(二進(jìn)制)結(jié)束。在做比較時(shí),應(yīng)該確保兩個(gè)表的字符排序相同。一般建表的時(shí)候不指定,可以走默認(rèn)的,全是默認(rèn)的就沒(méi)什么問(wèn)題了。
下面來(lái)模擬一下各種場(chǎng)景,表結(jié)構(gòu)如下(utf8默認(rèn)排序規(guī)則為utf8_general_ci):
mysql> show create table test.cs\G
*************************** 1. row ***************************
Table: cs
Create Table: CREATE TABLE `cs` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
查看表默認(rèn)排序規(guī)則集
mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.tables where table_name='cs';
+--------------+------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_COLLATION |
+--------------+------------+-----------------+
| test | cs | utf8_general_ci |
+--------------+------------+-----------------+
1 row in set (0.00 sec)
查看列排序規(guī)則集
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test | cs | id | NULL |
| test | cs | name | utf8_general_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)
從utf8升級(jí)為utf8mb4是不支持online ddl的,如下:
mysql> ALTER TABLE cs CONVERT TO CHARACTER SET utf8mb4,ALGORITHM=INPLACE,LOCK=NONE;
ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.
從utf8.utf8_general_ci變更為utf8.utf8_unicode_ci是不支持online ddl的,如下:
mysql> ALTER TABLE cs CONVERT TO CHARACTER SET utf8 collate utf8_unicode_ci,ALGORITHM=INPLACE,LOCK=NONE;
ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.
如果使用下面這種方式修改字符集,你會(huì)發(fā)現(xiàn),只更改了表級(jí)的,沒(méi)有更改列級(jí)的。
mysql> ALTER TABLE cs CHARACTER SET utf8 collate utf8_unicode_ci;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.tables where table_name='cs';
+--------------+------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_COLLATION |
+--------------+------------+-----------------+
| test | cs | utf8_unicode_ci |
+--------------+------------+-----------------+
1 row in set (0.00 sec)
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test | cs | id | NULL |
| test | cs | name | utf8_general_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)
所以真正改字符集的時(shí)候別忘了加上CONVERT TO,如下:
mysql> ALTER TABLE cs CONVERT TO CHARACTER SET utf8 collate utf8_unicode_ci;
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test | cs | id | NULL |
| test | cs | name | utf8_unicode_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)
要僅僅改變一個(gè)表的默認(rèn)字符集,應(yīng)使用此語(yǔ)句:
mysql> ALTER TABLE cs default CHARACTER SET utf8 collate utf8_general_ci,ALGORITHM=INPLACE,LOCK=NONE;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.tables where table_name='cs';
+--------------+------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_COLLATION |
+--------------+------------+-----------------+
| test | cs | utf8_general_ci |
+--------------+------------+-----------------+
1 row in set (0.00 sec)
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test | cs | id | NULL |
| test | cs | name | utf8_unicode_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)
可以發(fā)現(xiàn)列字符集沒(méi)有改變,并且只有新的列才會(huì)默認(rèn)繼承表的字符集(utf8.utf8_general_ci)。
總結(jié)
以上就是本文關(guān)于MySQL表排序規(guī)則不同錯(cuò)誤問(wèn)題分析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:幾個(gè)比較重要的MySQL變量、MySQL 聲明變量及存儲(chǔ)過(guò)程分析、MySQL主庫(kù)binlog(master-log)與從庫(kù)relay-log關(guān)系代碼詳解、MySQL prepare原理詳解等,有什么問(wèn)題可以隨時(shí)留言,互相交流,共同進(jìn)步。
您可能感興趣的文章:- MySQL中Order By多字段排序規(guī)則代碼示例
- Mysql中的排序規(guī)則utf8_unicode_ci、utf8_general_ci的區(qū)別總結(jié)