由于某些表情符号使用的 unicode 代码点超出了三字节 utf8 编码所能表示的范围,因此建议对用于存储表情符号的列使用 utf8mb4.
替代方案是 ucs16. ucs16le 和 ucs32.
例如:
mysql> CREATE TABLE emoji (id int unsigned NOT NULL PRIMARY KEY, val varchar(12) NOT NULL) DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected (0.57 sec) -- Insert a value containing the emojis "high voltage sign" and "face screaming in fear": mysql> INSERT INTO emoji VALUES (1, CONCAT(0xE29AA1, ' is scary ', 0xF09F98B1)); Query OK, 1 row affected (0.07 sec) mysql> SET SESSION character_set_results = utf8mb4; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM emoji; +----+-------------------+ | id | val | +----+-------------------+ | 1 | ⚡ is scary �� | +----+-------------------+ 1 row in set (0.00 sec)
如果终端支持显示表情符号,上面的输出应该类似于:
⚡ is scary ��
请务必使用应用程序所需的所有表情符号进行测试。
一些表情符号如 ⚡ 可以存储在 MySQL 的 3 字节 utf8 编码中,但其他表情符号如 害怕的脸,尖叫则不能。
日期:2020-09-17 00:11:33 来源:oir作者:oir