如何使用 utf8mb4 在 MySQL 中按表情符号搜索?

How can I search by emoji in MySQL using utf8mb4?

请帮助我了解如何在 MySQL utf8mb4 字段中处理表情符号等多字节字符。

请参阅下面的简单测试 SQL 来说明挑战。

/* Clear Previous Test */
DROP TABLE IF EXISTS `emoji_test`;
DROP TABLE IF EXISTS `emoji_test_with_unique_key`;

/* Build Schema */
CREATE TABLE `emoji_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `string` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `emoji_test_with_unique_key` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `string` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_string_status` (`string`,`status`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

/* INSERT data */
# Expected Result is successful insert for each of these.
# However some fail. See comments.
INSERT INTO emoji_test (`string`, `status`) VALUES ('', 1);                   # SUCCESS
INSERT INTO emoji_test (`string`, `status`) VALUES ('', 1);                   # SUCCESS
INSERT INTO emoji_test (`string`, `status`) VALUES ('', 1);                 # SUCCESS
INSERT INTO emoji_test (`string`, `status`) VALUES ('', 1);                 # SUCCESS
INSERT INTO emoji_test_with_unique_key (`string`, `status`) VALUES ('', 1);   # SUCCESS
INSERT INTO emoji_test_with_unique_key (`string`, `status`) VALUES ('', 1);   # FAIL: Duplicate entry '?-1' for key 'idx_string_status'
INSERT INTO emoji_test_with_unique_key (`string`, `status`) VALUES ('', 1); # SUCCESS
INSERT INTO emoji_test_with_unique_key (`string`, `status`) VALUES ('', 1); # FAIL: Duplicate entry '??-1' for key 'idx_string_status'

/* Test data */

    /* Simple Table */
SELECT * FROM emoji_test WHERE `string` IN ('','','',''); # SUCCESS (all 4 are found)
SELECT * FROM emoji_test WHERE `string` IN ('');                     # FAIL: Returns both  and 
SELECT * FROM emoji_test WHERE `string` IN ('');                     # FAIL: Returns both  and 
SELECT * FROM emoji_test;                                              # SUCCESS (all 4 are found)

    /* Table with Unique Key */
SELECT * FROM emoji_test_with_unique_key WHERE `string` IN ('','','',''); # FAIL: Only 2 are found (due to insert errors above)
SELECT * FROM emoji_test_with_unique_key WHERE `string` IN ('');                     # SUCCESS
SELECT * FROM emoji_test_with_unique_key WHERE `string` IN ('');                     # FAIL:  found instead of 
SELECT * FROM emoji_test_with_unique_key;                                              # FAIL: Only 2 records found ( and )

我有兴趣了解导致上述 FAIL 的原因以及如何解决这个问题。

具体来说:

  1. 为什么选择一个多字节字符 return 结果是 any 个多字节字符?
  2. 如何配置索引来处理多字节字符而不是 ?
  3. 您能否建议对上面的第二个 CREATE TABLE(具有唯一键的那个)进行更改,使所有测试查询 return 成功?

您对列使用 utf8mb4_unicode_ci,因此检查不区分大小写。如果您改用 utf8mb4_bin,则表情符号 和 会被正确识别为不同的字母。

使用 WEIGHT_STRING 您可以获得用于对输入字符串进行排序和比较的值。

如果你写:

SELECT
  WEIGHT_STRING ('' COLLATE 'utf8mb4_unicode_ci'),
  WEIGHT_STRING ('' COLLATE 'utf8mb4_unicode_ci')

然后可以看到都是0xfffd。在 Unicode Character Sets 他们说:

For supplementary characters in general collations, the weight is the weight for 0xfffd REPLACEMENT CHARACTER.

如果你写:

SELECT 
  WEIGHT_STRING('' COLLATE 'utf8mb4_bin'),
  WEIGHT_STRING('' COLLATE 'utf8mb4_bin')

您将获得它们的 unicode 值 0x01f32e0x01f336

对于 ÄÁA 等其他字母,如果您使用 utf8mb4_unicode_ci,它们是相等的,区别可以在:

中看到
SELECT
  WEIGHT_STRING ('Ä' COLLATE 'utf8mb4_unicode_ci'),
  WEIGHT_STRING ('A' COLLATE 'utf8mb4_unicode_ci')

那些映射到权重 0x0E33

Ä: 00C4  ; [.0E33.0020.0008.0041][.0000.0047.0002.0308] # LATIN CAPITAL LETTER A WITH DIAERESIS; QQCM
A: 0041  ; [.0E33.0020.0008.0041] # LATIN CAPITAL LETTER A

根据 : the weights used for utf8mb4_unicode_ci are based on UCA 4.0.0 因为 emoji 没有出现在那里,映射的权重是 0xfffd

如果您需要对常规字母和表情符号进行不区分大小写的比较和排序,则可以使用 utf8mb4_unicode_520_ci:

解决此问题
SELECT
  WEIGHT_STRING('' COLLATE 'utf8mb4_unicode_520_ci'),
  WEIGHT_STRING('' COLLATE 'utf8mb4_unicode_520_ci')

这些表情符号 0xfbc3f32e0xfbc3f336 也会有不同的权重。

不需要去举重。做这样的事情来查看两个字符(或字符串)是否相等。

mysql> SELECT '' = '' COLLATE utf8mb4_unicode_ci;
+--------------------------------------+
| '?' = '?' COLLATE utf8mb4_unicode_ci |
+--------------------------------------+
|                                    1 |  1 = true, hence equal
+--------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT '' = '' COLLATE utf8mb4_unicode_520_ci;
+------------------------------------------+
| '?' = '?' COLLATE utf8mb4_unicode_520_ci |
+------------------------------------------+
|                                        0 |  unequal
+------------------------------------------+
1 row in set (0.00 sec)