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

标签 mysql sql emoji 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. 为什么选择一个多字节字符会返回任何个多字节字符的结果?
  2. 如何配置索引来处理多字节字符而不是
  3. 您能否建议对上面的第二个 CREATE TABLE(具有唯一键的那个)进行更改,以使所有测试查询都成功返回?

最佳答案

您对列使用 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

如果您使用 utf8mb4_unicode_ci,对于其他字母,例如 ÄÁA,它们的区别可见:

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

根据:Difference between utf8mb4_unicode_ci and utf8mb4_unicode_520_ci collations in MariaDB/MySQL? utf8mb4_unicode_ci 的权重基于 UCA 4.0.0因为那里没有出现表情符号,所以映射的权重是 0xfffd

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

SELECT
  WEIGHT_STRING('🌮' COLLATE 'utf8mb4_unicode_520_ci'),
  WEIGHT_STRING('🌶' COLLATE 'utf8mb4_unicode_520_ci')

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

关于mysql - 如何使用 utf8mb4 在 MySQL 中按表情符号搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41147829/

相关文章:

sql - 如何在内部查询列中使用外部查询列

css - Unicode 表情符号在浏览器中不可见(显示为正方形)

php - 如何在 Laravel 5.1 中编写原始查询?

sql - 查找同一个人的最新约会日期

php - 预约确定

php - 在 PHP 中处理表情符号

mysql - phpMyAdmin mysql 将表情符号保存为问号

java - 无法通过java类连接到数据库

Mysql统计一个表中的不同用户

php - 登录成功后运行另一个查询