当前位置:网站首页>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 ~
边栏推荐
- 文件傳輸協議--FTP
- SQL 注入繞過(二)
- Anonymous Mount & named mount
- 关于st-link usb communication error的解决方法
- 【方块编码】基于matlab的图像方块编码仿真
- Machine learning (x) reinforcement learning
- 【历史上的今天】6 月 6 日:世界 IPv6 启动纪念日;《俄罗斯方块》发布;小红书成立
- Architecture high reliability application knowledge map ----- microservice architecture map
- 【历史上的今天】6 月 2 日:苹果推出了 Swift 编程语言;电信收购联通 C 网;OS X Yosemite 发布
- 把腾讯搬上云:云服务器 CVM 的半部进化史
猜你喜欢

Complex and inefficient logistics? Three steps to solve problems in enterprise administration

General timer and interrupt of stm32

SQL injection bypass (3)

简单文件传输协议TFTP

数仓的字符截取三胞胎:substrb、substr、substring

Jenkins - Pipeline syntax

Opencv——几何空间变换(仿射变换和投影变换)

STM32的通用定时器与中断

【历史上的今天】6 月 8 日:万维网之父诞生;PHP 公开发布;iPhone 4 问世

Machine learning (x) reinforcement learning
随机推荐
【历史上的今天】6 月 20 日:MP3 之父出生;富士通成立;谷歌收购 Dropcam
一种低成本增长私域流量,且维护简单的方法
Opencv——霍夫变换以及遇到的一些问题
架构高可靠性应用知识图谱 ----- 架构演进之路
Cvpr22 collected papers | hierarchical residual multi granularity classification network based on label relation tree
STM32F103的11个定时器
4G-learn from great partners
ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)
Data governance and data standards
我今天忘带手机了
系统管理员设置了系统策略,禁止进行此安装。解决方案
SQL 注入绕过(二)
【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试
Graduation summary
Complex and inefficient logistics? Three steps to solve problems in enterprise administration
Dynamic Host Configuration Protocol
NER中BiLSTM-CRF解读Forward_algorithm
Architecture high reliability application knowledge map ----- microservice architecture map
SQL 注入繞過(二)
简单文件传输协议TFTP