运算符喜欢,字段类型时间戳和西里尔字母? MySQL Bug?
作者:互联网
好像是MySQL Bug;
请求:
SELECT *
FROM table
WHERE (
id LIKE '%тест 199%'
OR `user` LIKE '%тест 199%'
OR `user_datetime` LIKE '%тест 199%'
OR `user_comments` LIKE '%тест 199%' )
ORDER BY id desc
LIMIT 0, 10
[Err] 1271-操作“赞”的排序规则非法混合
而当我们使用拉丁语.
请求:
SELECT *
FROM table
WHERE (
id LIKE '%test 199%'
OR `user` LIKE '%test 199%'
OR `user_datetime` LIKE '%test 199%'
OR `user_comments` LIKE '%test 199%' )
ORDER BY id desc
LIMIT 0, 10
请求成功;
怎么处理呢?
我所有的请求都是自动生成的,我无法更改逻辑coz函数生成器,因此有很多依赖性.
设定:
SET NAMES utf8
Character set utf8 -- UTF-8 Unicode
Collation utf8_general_ci
UPD的@eggyal
Request:
SHOW CREATE TABLE `comments`
Response:
CREATE TABLE `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(255) NOT NULL DEFAULT '',
`user_datetime` timestamp NULL DEFAULT NULL,
`user_comments` varchar(128) DEFAULT NULL,
UNIQUE KEY `id` (`id`) USING BTREE,
KEY `user_comments` (`user_comments`),
) ENGINE=InnoDB AUTO_INCREMENT=128456 DEFAULT CHARSET=utf8
MySQL 5.5.10版
解决方法:
您正在使用哪个版本的MySQL?如the manual中所述:
As of MySQL 5.5.3, implicit conversion of a numeric or temporal value to string produces a value that has a character set and collation determined by the 07001 and 07002 system variables. (These variables commonly are set with 07003. For information about connection character sets, see 07004.)
This change means that such a conversion results in a character (nonbinary) string (a 07005, 07006, or 07007 value), except when the connection character set is set to
binary
. In that case, the conversion result is a binary string (a 07008, 07009, or 070010 value).Before MySQL 5.5.3, an implicit conversion always produced a binary string, regardless of the connection character set. Such implicit conversions to string typically occur for functions that are passed numeric or temporal values when string values are more usual, and thus could have effects beyond the type of the converted value.
因此,如果您使用的MySQL版本早于5.5.3,而与SET NAMES无关,则使用LIKE运算符时将TIMESTAMP列隐式转换为字符串将始终导致二进制字符集的字符串(奇怪的是,这是sqlfiddle也是如此,它声称是5.5.20);由于无法将此类字符串与utf8字符集中的字符串进行比较,因此必须将user_datetime列显式转换为UTF-8字符串:
SELECT *
FROM `comments`
WHERE (
`id` LIKE '%тест 199%'
OR `user` LIKE '%тест 199%'
OR CONVERT(`user_datetime` USING utf8) LIKE '%тест 199%'
OR `user_comments` LIKE '%тест 199%'
)
ORDER BY `id` DESC
LIMIT 0, 10
标签:character-encoding,mysql,pdo,internationalization 来源: https://codeday.me/bug/20191201/2080486.html