gpt4 book ai didi

mysql - 运算符喜欢,字段类型时间戳和西里尔字母? MySQL错误?

转载 作者:可可西里 更新时间:2023-11-01 08:21:02 24 4
gpt4 key购买 nike

似乎是 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 - 操作“like”的排序规则混合非法

当我们使用拉丁语时。要求:

        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

请求成功;

如何处理?

我所有的请求都是自动生成的,我不能改变逻辑因为函数生成器有很多依赖。

设置:

SET NAMES utf8
Character set utf8 -- UTF-8 Unicode
Collation utf8_general_ci

@eggyal 的UPD

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 character_set_connection and collation_connection system variables. (These variables commonly are set with SET NAMES. For information about connection character sets, see Section 10.1.4, “Connection Character Sets and Collations”.)

This change means that such a conversion results in a character (nonbinary) string (a CHAR, VARCHAR, or LONGTEXT value), except when the connection character set is set to binary. In that case, the conversion result is a binary string (a BINARY, VARBINARY, or LONGBLOB 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.

因此,当使用 LIKE 运算符时,将 TIMESTAMP 列隐式转换为字符串将始终导致字符串binary 字符集,如果您使用的 MySQL 版本早于 5.5.3,则不考虑 SET NAMES(奇怪的是,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

关于mysql - 运算符喜欢,字段类型时间戳和西里尔字母? MySQL错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10379299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com