当前位置:网站首页>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.username
The 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 = 1
such , 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 ~
边栏推荐
- 虽然TCGA数据库有33种癌症
- Zero foundation self-study SQL course | case function
- Elk in Windows Environment - logstash+mysql (4)
- Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
- 線程池實現:信號量也可以理解成小等待隊列
- flutter系列之:flutter中的变形金刚Transform
- Cornernet由浅入深理解
- Storage structure of graph
- 圖的存儲結構
- How to select documents for literature review? For example, I can't finish reading more than 200 search results. How to select documents
猜你喜欢
随机推荐
积分体系和营销活动结合在一起有哪些玩法
线程池实现:信号量也可以理解成小等待队列
使用cef3开发的浏览器不支持flash问题的解决
After a period of silence, I came out again~
How to find Chinese documents corresponding to foreign documents?
计数质数[枚举 -> 空间换时间]
数仓的字符截取三胞胎:substrb、substr、substring
吴恩达《机器学习》课程总结(14)_降维
MySQL read / write separation configuration
Structure de stockage des graphiques
c语言字符指针、字符串初始化问题
[PCL self study: pclplotter] pclplotter draws data analysis chart
刚开始看英文文献,想问一下各位,最初应该怎么看进去?
MySQL enterprise parameter tuning practice sharing
100 questions for an enterprise architect interview
赛尔笔记|视频文本预训练简述
【论文阅读|深读】SDNE:Structural Deep Network Embedding
一文剖析C语言函数
Grab those duplicate genes
【AI应用】Jetson Xavier NX的详情参数