当前位置:网站首页>MySQL details - aggregation and grouping
MySQL details - aggregation and grouping
2022-06-29 01:59:00 【wuqiongjin】
1. Aggregate functions
| Aggregate functions | function |
|---|---|
| count() | Count the number of rows of records that meet the conditions |
| max() | Find the maximum value of records that meet the conditions |
| min() | Find the minimum value of records that meet the conditions |
| sum() | Count the sum of the values of the records that meet the conditions |
| avg() | Count the average of the values of the records that meet the conditions |
Be careful :
countUse of functions :
count(*)、count(id)、count(1).
count(*) It can count the total number of records , and count(id) It's statistics id The number of records with non null value of column , That is, their values may be different . Extended reading : MySQL:count(*),count( Field ),count(1) The difference betweenIn the query statement of the aggregate function , You cannot query fields other than aggregate functions and grouping fields , Because it doesn't make sense ( Generally, the database will report an error ).
select max(age) from stu√select max(age),name from stu×Aggregate functions generally cooperate with
group byUse it togetherAggregate function is matching
group bywhen , Group fields can be queried additionally .select max(age), gender from stu group by gender√
2. Group query
Grouping query is to make grouping statistics on the original data .
select Group column name 1, Group list 2, Aggregate functions 1, Aggregate functions 2 from Table name
group by Group column name 1, Group column name 2;
From the format we can see , Groups can be grouped according to multiple fields .
• The condition screening after grouping
select Group column name , Aggregate functions from Table name
[where Conditions 1]
group by Group column name
[having Conditions 2( You can judge by the value of the aggregate function )];
We can use where Filter by conditions , It can also be used. having Filter by conditions . however The location of the two and the logic of the code execution are different !
Be careful :
- select The fields that appear in , Must be in a group .
- having The conditional judgment fields appearing in the clause , It must also be in the group .
Example :
-- 1
select gender, max(age) from student
where gender=' male '
group by gender;
-- 2
select gender, max(age) from student
group by gender
having gender=' male ';
For example 1,where It's right from and select The structure of , After this layer of screening , Re execution group by Grouping .
For example 2,having It's right group by Screening , stay group by After execution , And then screen .
SQL The execution order of each keyword in the query :
from > on > join > where > group by > with > having > select > distinct > order by > limit
边栏推荐
- Design and development of VB mine sweeping game
- 【Redis】List类型
- Business system anti-virus
- 想请教一下,我在中山,到哪里开户比较好?在线开户是安全么?
- 【Redis】Key的层级结构
- 如何成为一名高级数字 IC 设计工程师(6-5)数字 IC 验证篇:覆盖率收集
- Blog publishing test 2
- In MySQL database, the two data written when creating tables with foreign keys are the same. Do I copy them or fail to display them
- 利用kubernetes資源鎖完成自己的HA應用
- Analysis of parsing principle of OData metadata request response in SAP ui5 application
猜你喜欢

SystemVerilog-结构体(一)

Typescript (5) class, inheritance, polymorphism

【Redis】Key的层级结构

The left toolbar of hbuilder is missing

Exclusive analysis | real situation of software test about resume and interview
Fundamentals of scala (3): operators and process control

HBuilder左侧工具栏不见了

我把整个研发中台拆分过程的一些心得总结

Introduction to UE gameplay 44 (animation import FBX and production standard)

Interviewer: with the for loop, why do you need foreach??
随机推荐
AHA C language, C language programming introductory books and PPT (PDF version) download website
数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(1)2022 紫光展锐(上)
Typescript (5) class, inheritance, polymorphism
【Redis】SortedSet类型
如何成为一名高级数字 IC 设计工程师(4-3)脚本篇:C 语言实现的文件读写操作
Fundamentals of scala (3): operators and process control
Three tips from small video app source code developers on small video platforms
Server antivirus
Near's storage stacking
为什么建议大家尽可能选择自研公司
Blog publishing test 3
Introduction to super dongle scheme
PR FAQ: how to retrieve accidentally deleted video and audio in PR?
Analysis of sending principle of OData metadata request for SAP ui5 application
如何成为一名高级数字 IC 设计工程师(6-4)数字 IC 验证篇:测试点分解
[redis] set type
[redis] key hierarchy
MySQL详解 --- 聚合与分组
[C language] Fibonacci sequence and frog jumping steps (the most detailed elementary frog jumping steps)
[redis] sortedset type