当前位置:网站首页>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
边栏推荐
- Server antivirus
- Large scale visual relationship understanding
- How to manage device authorization
- 大三下期末考试
- Scala 基礎 (三):運算符和流程控制
- Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused
- ASP. Design and implementation of net+sql online alumni list
- 如何成为一名高级数字 IC 设计工程师(1-1)Verilog 编码语法篇:引言
- PR FAQ: how to retrieve accidentally deleted video and audio in PR?
- How to encrypt the device
猜你喜欢
Scala Foundation (3): Operators and Process Control

The left toolbar of hbuilder is missing

SAP ui5 beginner tutorial 24 - how to use OData data model

In simple terms, server intrusion prevention

Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused

SAP ui5 beginner tutorial 22 - development and use of filter

HBuilder左侧工具栏不见了

Uniapp notes

Oculusrifts and unity UI interaction (1) - Overview

SystemVerilog-结构体(一)
随机推荐
Share the code technology points and software usage of socket multi client communication
栈的增删查改---动态内存
基于 RISC-V SoC 的可配置 FFT 系统设计(1)引言
Magic Quadrant of motianlun's 2021 China Database
Stm32l4xx serial port log configuration analysis
同花顺究竟是如何开户?在线开户是安全么?
如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作
[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB
【Redis】数据介绍 & 通用命令 & String类型
想请教股票开户要认识谁?现在网上开户安全么?
SQL splits strings into tables
How to become a senior digital IC Design Engineer (1-1) Verilog coding Grammar: Introduction
Test a CSDN free download software
0和1的歧义问题
Analysis of advantages and disadvantages of environment encryption and transparent encryption
Which is the best billing method for okcc call center
Why is it recommended that you choose the self research company as much as possible
[TS] type alias
Design and development of VB mine sweeping game
How to use PN junction to measure temperature?