Rya*_*yan 16 mysql sql emoji utf8mb4
请帮助我理解在MySQL utf8mb4字段中如何处理像emoji这样的多字节字符.
请参阅下面的简单测试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 )
Run Code Online (Sandbox Code Playgroud)
我有兴趣了解导致上述问题FAIL的原因以及如何解决这个问题.
特别:
??CREATE TABLE(具有唯一键的键)以使所有测试查询成功返回?t.n*_*ese 15
您 utf8mb4_unicode_ci用于列,因此检查不区分大小写.如果你使用utf8mb4_bin,那么表情符号并被正确识别为不同的字母.
有了WEIGHT_STRING你可以得到被用于排序和输入字符串比较值.
如果你写:
SELECT
WEIGHT_STRING ('' COLLATE 'utf8mb4_unicode_ci'),
WEIGHT_STRING ('' COLLATE 'utf8mb4_unicode_ci')
Run Code Online (Sandbox Code Playgroud)
然后你可以看到两者都是0xfffd.在Unicode字符集中,他们说:
对于一般排序规则中的补充字符,权重是0xfffd REPLACEMENT CHARACTER的权重.
如果你写:
SELECT
WEIGHT_STRING('' COLLATE 'utf8mb4_bin'),
WEIGHT_STRING('' COLLATE 'utf8mb4_bin')
Run Code Online (Sandbox Code Playgroud)
您将获得他们的unicode值0x01f32e,0x01f336而不是.
对于其他的字母一样Ä,Á而且A,如果你使用相等utf8mb4_unicode_ci,差异可以看出:
SELECT
WEIGHT_STRING ('Ä' COLLATE 'utf8mb4_unicode_ci'),
WEIGHT_STRING ('A' COLLATE 'utf8mb4_unicode_ci')
Run Code Online (Sandbox Code Playgroud)
那些映射到重量 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
Run Code Online (Sandbox Code Playgroud)
根据:MariaDB/MySQL中utf8mb4_unicode_ci和utf8mb4_unicode_520_ci排序的区别?用于的权重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')
Run Code Online (Sandbox Code Playgroud)
也将获得这些表情图案不同的权重0xfbc3f32e和0xfbc3f336.