ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2019-12-01 11:15:03  阅读:287  来源: 互联网

标签:character-encoding mysql pdo internationalization


好像是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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有