I am trying to select unicdode character /u2028 in MySQL 5.1. MySQL 5.1 does support utf8 and ucs2.
我试图在MySQL 5.1中选择unicdode character / u2028。 MySQL 5.1确实支持utf8和ucs2。
In newer versions of MySQL i could select the char just be using utf16 or utf32 collation:
在较新版本的MySQL中,我可以选择使用utf16或utf32排序规则的char:
SELECT char(0x2028 using utf16);
SELECT char(0x00002028 using utf32);
But MySQL 5.1 do not support utf16 and utf32. How could I select the unicode character then?
但MySQL 5.1不支持utf16和utf32。我怎么能选择unicode字符呢?
Perhaps a few words about my use case: I have an third party application which stores data in a mysql database and using JavaScript for user interface. The application do not deal with the problem unicode characters /u2028 and /u2029 are valid JSON but will break JavaScript code. (For details see http://timelessrepo.com/json-isnt-a-javascript-subset) So I like to know how much data is affected by that issue and perhaps use replace on MySQL to fix it.
也许关于我的用例的几句话:我有一个第三方应用程序,它将数据存储在mysql数据库中,并使用JavaScript作为用户界面。该应用程序不处理问题unicode字符/ u2028和/ u2029是有效的JSON但会破坏JavaScript代码。 (有关详细信息,请参阅http://timelessrepo.com/json-isnt-a-javascript-subset)所以我想知道有多少数据受到该问题的影响,并且可能使用MySQL上的替换来修复它。
To demonstrate the problem:
为了证明这个问题:
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`string` varchar(100) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `test` (`id`, `string`) VALUES
(1, 'without U+2028'),
(2, 'with U+2028 at this "
"point');
SELECT * FROM test WHERE string LIKE CONCAT("%", char(0x2028 using utf16), "%");
// returns row 2 as expected
SELECT * FROM test WHERE string LIKE CONCAT("%", char(??? using utf8), "%");
// U+2028 in utf8 is 0xE2 0x80 0xA8 isn't it?
// But how to parse this to char function?
1 个解决方案
#1
4
The unicode character U+2028 can be encoded in UTF-8 as hexadecimal e280a8. So the answer is to use the UNHEX function in MySQL to find it.
unicode字符U + 2028可以UTF-8编码为十六进制e280a8。所以答案是使用MySQL中的UNHEX函数来查找它。
SELECT * FROM test WHERE string LIKE CONCAT("%", UNHEX('e280a8'), "%");
MySQL 5.1 can only handle characters encloded in UTF-8 up to three bytes long. So searching for U+2028 using UNHEX will work, but searching for U+1F600 won't as that takes up four bytes.
MySQL 5.1只能处理UTF-8中长达三个字节的字符。因此,使用UNHEX搜索U + 2028会起作用,但搜索U + 1F600不会占用四个字节。
Use UNHEX('e280a9') to search for U+2029.
使用UNHEX('e280a9')搜索U + 2029。
#1
4
The unicode character U+2028 can be encoded in UTF-8 as hexadecimal e280a8. So the answer is to use the UNHEX function in MySQL to find it.
unicode字符U + 2028可以UTF-8编码为十六进制e280a8。所以答案是使用MySQL中的UNHEX函数来查找它。
SELECT * FROM test WHERE string LIKE CONCAT("%", UNHEX('e280a8'), "%");
MySQL 5.1 can only handle characters encloded in UTF-8 up to three bytes long. So searching for U+2028 using UNHEX will work, but searching for U+1F600 won't as that takes up four bytes.
MySQL 5.1只能处理UTF-8中长达三个字节的字符。因此,使用UNHEX搜索U + 2028会起作用,但搜索U + 1F600不会占用四个字节。
Use UNHEX('e280a9') to search for U+2029.
使用UNHEX('e280a9')搜索U + 2029。