当前位置:网站首页>'only_ full_ group_ The influence of by'sql mode on group by and its treatment
'only_ full_ group_ The influence of by'sql mode on group by and its treatment
2022-06-29 06:22:00 【jcuser】
「 This is my participation 2022 For the first time, the third challenge is 7 God , Check out the activity details :2022 For the first time, it's a challenge 」
Preface
Some time ago, system exceptions were found during project development , Open the log and find the following error : Search for relevant information , This is because mysql Database sql_mode=only_full_group_by Open the , Years ago, the product was in a hurry to go online , So the team leader said to contact first DBA hold sql_mode=only_full_group_by Turn off the , Later change .
therefore , Today, the team leader entrusted the task to me .
ONLY_FULL_GROUP_BY Official statement :
Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.
Reject selection list 、 HAVING Condition or ORDER BY List references queries for non aggregated Columns , These columns are not in GROUP BY Clause is named , Nor is it functionally dependent on ( The only certainty ) GROUP BY Column .( Google Translate )
terms of settlement
Official documents :MySQL Yes GROUP BY To deal with
1. close sql_mode=only_full_group_by
2. It's official :You can achieve the same effect without disabling ONLY_FULL_GROUP_BY by using ANY_VALUE() to refer to the nonaggregated column. You can't help but ONLY_FULL_GROUP_BY use In the case of ANY_VALUE() Reference non aggregated columns to achieve the same effect .
Problem recurrence
Let's do a small example to repeat this error :
Data preparation
- Create table
CREATE TABLE `emp`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`age` INT(3),
`salary` DOUBLE(10,2),
PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy code - insert data
INSERT INTO `emp` VALUE(null,' Zhang San ',21,10000);
INSERT INTO `emp` VALUE(null,' Li Si ',22,15000);
INSERT INTO `emp` VALUE(null,' Wang Wu ',23,20000);
INSERT INTO `emp` VALUE(null,' Zhao Liu ',24,25000);
INSERT INTO `emp` VALUE(null,' Qian Qi ',21,10000);
INSERT INTO `emp` VALUE(null,' Sun Ba ',22,15000);
INSERT INTO `emp` VALUE(null,' Li Jiu ',23,20000);
INSERT INTO `emp` VALUE(null,' Feng Shi ',24,25000);
Copy code see sql_mode
Use the following sql see sql Whether the pattern contains ONLY_FULL_GROUP_BY
SELECT @@session.sql_mode;
Copy code You can see sql_mode Inside it is opened ONLY_FULL_GROUP_BY Of .
If it is not enabled, you can use the following command to enable :
SET SESSION sql_mode = sys.list_add(@@session.sql_mode, 'ONLY_FULL_GROUP_BY');
close :
SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY');
My current sql Pattern ONLY_FULL_GROUP_BY Is open , Let's test it GROUP_BY
sql example
First, let's make a correct query :
SELECT age, Max(salary) FROM emp GROUP BY age;
Copy code It has no effect on aggregate columns , Only non aggregated columns are checked for the existence of and GROUP BY list in .
The following is an incorrect query :
SELECT id,`name` age, Max(salary) FROM emp GROUP BY age;
Copy code 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sql_mode_test.emp.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
terms of settlement :
- modify sql_mode remove ONLY_FULL_GROUP_BY To configure
SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY');
Copy code Query again :
- Give the non aggregate column non Group By Column plus function ANY_VALUE( Non aggregate Columns )
SELECT ANY_VALUE(id) as id,ANY_VALUE(name) as name ,age, Max(salary) FROM emp GROUP BY age;
Copy code Obviously, these two methods can make the program not report errors , But here id and name and It's not certain Unique value , It makes no sense , Just select one of the qualified groups to fill in .
- stay GROUP BY Followed by select Non aggregated columns in the list , This method solves the problem of error reporting but changes sql Query results , So it doesn't apply
SELECT id, `name` age, Max(salary) FROM emp GROUP BY age,id,name;
Copy code * One thing to note is that , When open the ONLY_FULL_GROUP_BY Pattern , When the query does not group by sentence ,select When the list has an aggregate function , Will report a mistake
SELECT `name`, MAX(age) FROM emp;
Copy code 1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'sql_mode_test.emp.name'; this is incompatible with sql_mode=only_full_group_by
Here you can use ANY_VALUE() To solve :
SELECT ANY_VALUE(`name`) as name , MAX(age) FROM emp;
Copy code Summary :
sql Mode on ONLY_FULL_GROUP_BY when , If there is GROUP BY, A non aggregated column must be GROUP BY Columns in the list , If not GROUP BY The next column , have access to ANY_VALUE(column) Convert to aggregate column .
without GROUP BY,SELECT Aggregate column and non aggregate column cannot exist at the same time , If you want to exist at the same time, you can use ANY_VALUE(column) Convert to aggregate column .
Back to the project
see mapper file , I looked at the contents GROUP BY Query for sql, See this SELECT ,SELECT All fields .
So much to change ,ONLY_FULL_GROUP_BY It must not be closed , Can only be changed sql 了 , So I wonder if there is any way to automatically modify the query , add ANY_VALUE(), So I thought of interceptors , Just right ,mybits Provides interceptors , It can be done to sql To deal with , So I began to try , That's all for today , My solution will be updated later . If you have a better way, you can also leave your valuable comments in the comment area .
边栏推荐
- Pytest (7) -yield and termination function
- Problems with MySQL database query
- It turns out that the joys and sorrows of programmers are not interlinked
- Personal blog item: processing of reading number +1 after viewing article details
- Quickly write MVVM code using source generators
- Why is there a packaging type?
- Benign competition will promote each other
- 2022 recommended high-speed rail industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
- Conditional test, if and case conditional test statements of shell script
- QT (x): innosetup for software packaging
猜你喜欢

Jenkins operation Chapter 6 mail server sending build results

Call the computer calculator and use it to convert several base numbers

Sum of digits under k-ary representation of leetcode simple problem

Monitor employee turnover dynamics. This system makes employees tremble!

Design and practice of kubernetes cluster and application monitoring scheme

Part 63 - interpreter and compiler adaptation (II)

Are there too many programmers in China at present?
![[deep learning] - maze task learning I (to realize the random movement of agents)](/img/c1/95b476ec62436a35d418754e4b11dc.jpg)
[deep learning] - maze task learning I (to realize the random movement of agents)

Fault: NetBt log for id4321

Ctrip launched the "3+2" office mode. Are you sour?
随机推荐
Problems with MySQL database query
Functions and arrays of shell scripts
Personal blog item: processing of reading number +1 after viewing article details
Haar cascades and LBP cascades in face detection [closed] - Haar cascades vs. LBP cascades in face detection [closed]
The most complete machine learning model training process
Fault: administrator account cannot be selected for login
Stack -- 739 Daily temperature
Design of leetcode simple problem goal parser
5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin ((tdmpp) H2) /2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin copper (no2tdmpp) Cu) supplied by Qiyue
[deep learning] - maze task learning I (to realize the random movement of agents)
2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin
What are the uses of wireless pressure collectors?
2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin copper (no2tmpp) Cu) /2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin (no2tmpp) H2) Qiyue porphyrin supply
Programming specification and variables of shell script
Hyperledger Fabric 2. X custom smart contract
64 commonly used terms for data analysis, really all!
Research Report on the recommended lithography industry in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report is continuously u
2022-01 Microsoft vulnerability notification
Sum of digits under k-ary representation of leetcode simple problem
Love that can't be met -- what is the intimate relationship maintained by video chat