Conditional query of Mysql database (aggregate function, group query)

Conditional query of Mysql database

One, aggregate function
1. Introduction to
aggregate functions Aggregate functions are also called group functions. They usually perform statistics and calculations on the data in the table, and are generally used in combination with group by to count and calculate grouped data.

Commonly used aggregate functions:

  1. count(col): Indicates the total number of rows in the specified column
  2. max(col): Indicates the maximum value of the specified column
  3. min(col): Indicates the minimum value of the specified column
  4. sum(col): Indicates the sum of the specified column
  5. avg(col): Means to find the average value of the specified column

2. Find the total number of rows

-- 返回非NULL数据的总行数.
select count(height) from students; 
-- 返回总行数,包含null值记录;
select count(*) from students;

3. Find the maximum

-- 查询女生的编号最大值
select max(id) from students where gender = 2;

4. Find the minimum

-- 查询未删除的学生最小编号
select min(id) from students where is_delete = 0;

5. Sum

-- 查询男生的总身高
select sum(height) from students where gender = 1;
-- 平均身高
select sum(height) / count(*) from students where gender = 1;

6. Find the average

-- 注意:求男生的平均身高, 聚合函数不统计null值,平均身高可能有误
select avg(height) from students where gender = 1;
-- 求男生的平均身高, 包含身高是null的
select avg(ifnull(height,0)) from students where gender = 1;
说明
ifnull函数: 表示判断指定字段的值是否为null,如果为空使用自己提供的值。

Note on
aggregate functions : The aggregate function ignores records whose fields are null by default. If you want the records whose column value is null to participate in the calculation, you must use the ifnull function to replace the null value.

Two, group query

  1. Introduction to
    grouping query Grouping query is to group the query results according to the specified field, and the data in the field are divided into a group.
分组查询基本的语法格式如下:

GROUP BY 列名 [HAVING 条件表达式] [WITH ROLLUP]
说明:
列名: 是指按照指定字段的值进行分组。
HAVING 条件表达式: 用来过滤分组后的数据。
WITH ROLLUP:在所有记录的最后加上一条记录,显示select查询时聚合函数的统计和计算结果
  1. The use of
    group by group by can be used for single field grouping or multiple field grouping
-- 根据gender字段来分组
select gender from students group by gender;
-- 根据name和gender字段进行分组
select name, gender from students group by name, gender;
  1. Use of group by + group_concat()
    group_concat (field name): Count the information collection of each grouping specified field, and use commas to separate each information
-- 根据gender字段进行分组, 查询gender字段和分组的name字段信息
select gender,group_concat(name) from students group by gender;
  1. group by + use of aggregate functions
-- 统计不同性别的人的平均年龄
select gender,avg(age) from students group by gender;
-- 统计不同性别的人的个数
select gender,count(*) from students group by gender;
  1. The
    having function of group by + having is similar to where to filter data, but having is to filter grouped data and can only be used for group by
-- 根据gender字段进行分组,统计分组条数大于2
select gender,count(*) from students group by gender having count(*)>2;
  1. The role of group by + with rollup
    with rollup is to add a new line after the last record to display the statistics and calculation results of the aggregate function in the select query
-- 根据gender字段进行分组,汇总总人数
select gender,count(*) from students group by gender with rollup;
-- 根据gender字段进行分组,汇总所有人的年龄
select gender,group_concat(age) from students group by gender with rollup;

to sum up:

  • count(col): Indicates the total number of rows in the specified column
  • max(col): Indicates the maximum value of the specified column
  • min(col): Indicates the minimum value of the specified column
  • sum(col): Indicates the sum of the specified column
  • avg(col): Means to find the average value of the specified column
  • group by group data according to one or more fields specified
  • The group_concat (field name) function is to count the information collection of each group specified field
  • When the aggregate function is used in combination with group by, the aggregate function counts and calculates the data of each group
  • having is to filter the packet data
  • with rollup adds a new line after the last record to display the statistics and calculation results of the aggregate function in the select query

Guess you like

Origin blog.csdn.net/li944254211/article/details/109262997