当前位置:网站首页>SQL reported an unusual error, which confused the new interns
SQL reported an unusual error, which confused the new interns
2022-06-28 02:39:00 【Huawei cloud developer Alliance】
Abstract : A few days ago, a very simple SQL Reported an unusual error .
This article is shared from Huawei cloud community 《 Remember a mysql Format conflict of association query 【 May 04】》, author : KevinQ .
Origin of problem
As CRUD The programmer , Of course, the most commonly used function is database query .
A few days ago, a very simple SQL Reported an unusual error :
Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='Let's have a look today mysql Of utf8mb4 Two format problems , And how to solve this problem ?
mysql Of utf8 Format and sorting rules
Character set uft8 And uft8mb4
stay mysql When creating tables and adding fields in , Want to choose utf8 There are two options for character set :
- utf8
- utf8mb4
What's the difference between the two ?
MySQL Is in 5.5.3 Then there was utf8mb4 Optional character set ,mb4 It means :Most Bytes 4, Compatible unicode.
and utf8 Most support 3 Bytes , such as 3 Bytes of uft8 Unable to support Emoji Expressions and unusual Chinese characters , And any new Unicode Character etc. , That's why uft8mb4.
Sort rule
Our problem this time is caused by collation conflict :
Query statement :
SELECT * FROM table1 t1 left join table2 on t1.username = t2.usernameThe coded character sets of the associated fields are utf8mb4, however t1.username The sorting rule is utf8mb4_general_ci, and t2.username The sorting rule is utf8mb4_unicode_ci.
What are the differences and characteristics between the two sorting rules ?
characteristic
utf8mb4_unicode_ci It's based on standards Unicode To sort and compare , That is, it can support all Unicode Precise sorting of characters ;
and utf8mb4_general_ci It didn't come true Unicode Sort rule , When encountering special characters , The order may be different .
difference
Therefore ,uft8mb4_general_ci comparison utf8mb4_unicode_ci, Although the accuracy of the former is discounted , But comparison and sorting are performed faster , also , Usually, the order in which special characters are encountered is not important .
Solution to collation incompatibility
stay mysql in , There are two solutions to collation incompatibility :
1、 The first one is , Modify the format of table fields to be consistent .
That is, the sorting rules of the modified table are unified as utf8mb4_unicode_ci, Or another . It is more recommended to use utf8mb4_unicode_ci Collation of , To borrow StackOverflow Last paragraph :
There is almost certainly no reason to use utf8mb4_general_ci anymore, as we have left behind the point where CPU speed is low enough that the performance difference would be important. Your database will almost certainly be limited by other bottlenecks than this.
Probably means to say , At present CPU The running speed of is fast enough that we can no longer consider this sort as a parameter , And other expenses should be opened .
2、 Another way is to use keywords ,COLLATE
adopt COLLATE attribute , You can specify how columns are sorted and compared .
When we use , Put it where the collation needs to be modified in the association query :
SELECT u.guidFROM `test` tLEFT JOIN user u ON u.guid = t.guid COLLATE utf8mb4_unicode_ciWHERE t.state = 1such , Use COLLATE attribute , You can use fields with different sorting rules to perform associated queries .
however , After testing , It will slow down like this SQL Query speed .
What is the specific way , Need careful consideration .
Click to follow , The first time to learn about Huawei's new cloud technology ~
边栏推荐
- Jenkins - accédez à la variable de paramètre personnalisée Jenkins, en traitant les espaces dans la valeur de la variable
- NER中BiLSTM-CRF解读Forward_algorithm
- Dynamic Host Configuration Protocol
- Mysql大合集,你要内容的这里全都有
- KVM相关
- 关于st-link usb communication error的解决方法
- Stm32f1 interrupt introduction
- SQL injection bypass (2)
- How to use data-driven "customer lifecycle management" to improve lead conversion rate and customer satisfaction?
- 【历史上的今天】6 月 6 日:世界 IPv6 启动纪念日;《俄罗斯方块》发布;小红书成立
猜你喜欢

文件传输协议--FTP

匿名挂载&具名挂载

MySQL优化小技巧

Ti am3352/54/59 industrial core board hardware specification

From how to use to how to implement a promise

【云原生】-Docker安装部署分布式数据库 OceanBase

【历史上的今天】6 月 3 日:微软推出必应搜索引擎;Larry Roberts 启动阿帕网;Visual Basic 之父出生

Dynamic Host Configuration Protocol

【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用

SQL injection bypass (2)
随机推荐
I forgot my cell phone today
High reliability application knowledge map of Architecture -- the path of architecture evolution
Opencv——几何空间变换(仿射变换和投影变换)
Dynamic Host Configuration Protocol
Keil “st-link usb communication error“解决方法
技术人员如何成为技术领域专家
Scoped attribute and lang attribute in style
系统管理员设置了系统策略,禁止进行此安装。解决方案
Flask基础:模板渲染+模板过滤使用+控制语句
Skills in schematic merging
设计电商秒杀系统
SQL 注入绕过(四)
Locust 性能测试 - 参数化,并发循环数据采样不重复
「大道智创」获千万级preA+轮融资,推出科技消费机器人
【二维码图像矫正增强】基于MATLAB的二维码图像矫正增强处理仿真
Architecture high reliability application knowledge map ----- microservice architecture map
SQL injection Bypass (2)
How to handle computer security certificate errors
Based on am335x development board arm cortex-a8 -- acontis EtherCAT master station development case
Complex and inefficient logistics? Three steps to solve problems in enterprise administration