当前位置:网站首页>SQL reported an unusual error, which confused the new interns
SQL reported an unusual error, which confused the new interns
2022-06-28 00:03: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.guid
FROM
`test` t
LEFT JOIN user u ON u.guid = t.guid COLLATE utf8mb4_unicode_ci
WHERE 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 ~
边栏推荐
- 如何找到外文文献对应的中文文献?
- 抓出那些重复的基因
- Zero foundation self-study SQL course | if function
- c语言之字符串数组
- 【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
- After a period of silence, I came out again~
- Const keyword and its function (usage), detailed explanation of C language const
- What are the ways to combine the points system with marketing activities
- How to quote Chinese documents when writing a foreign language?
- Feign通过自定义注解实现路径的转义
猜你喜欢
![[tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom](/img/a8/4786e82d0646b08c195dd0a17af227.png)
[tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom

什么是cookie,以及v-htm的安全性隐患

Instructions for vivado FFT IP

Storage structure of graph

halcon之区域:多种区域(Region)特征(6)

零基础自学SQL课程 | SQL基本函数大全

c语言字符指针、字符串初始化问题
Does the subscription of Siyuan notes stop deleting cloud data directly?

Chenyun pytorch learning notes_ Build RESNET with 50 lines of code

What if Fiddler fails to listen to the interface
随机推荐
Thread pool implementation: semaphores can also be understood as small waiting queues
Using two stacks to implement queues [two first in first out is first in first out]
[digital ic/fpga] detect the position of the last matching sequence
夏日的晚会
RecyclerView实现分组效果,多种实现方式
Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
Msp430f5529 MCU reads gy-906 infrared temperature sensor
【PCL自学:PCLPlotter】PCLPlotter绘制数据分析图
Flutter series: Transformers in flutter
Windows环境下的ELK——Logstash+Mysql(4)
Zero foundation self-study SQL course | complete collection of SQL basic functions
[AI application] detailed parameters of NVIDIA geforce RTX 1080ti
线程池实现:信号量也可以理解成小等待队列
SQL中IS NOT NULL与!=NULL的区别
Is it safe for Huatai Securities to open an account online?
Chapter 2 integrated mp
撰写外文时怎样引用中文文献?
At the beginning of reading English literature, I would like to ask you how you should read it in the first place?
[AI application] detailed parameters of NVIDIA geforce RTX 3060
flutter系列之:flutter中的变形金刚Transform