Table of Contents
1. Preface
2. Insert query results
3. Aggregation query
3.1 Aggregation function
3.1.1 count 
3.1.2 sum
3.1.3 avg 
3.1.4 max 和 min
4、GROUP BY 子句
5、HAVING 关键字
Home Database Mysql Tutorial How to use MySQL aggregate query method

How to use MySQL aggregate query method

May 27, 2023 pm 11:47 PM
mysql

    1. Preface

    The previous content has almost introduced the basic addition, deletion, modification and query, and also introduced the relevant constraints of the table. From this issue The content from the beginning will be more complicated, and there will be more complex query SQL.

    2. Insert query results

    Queries are still used more often. For the queried Can the data also be saved? That is, insert the query results into another table.

    Case: Create a student table with fields such as id, name, sex, java, and python. Now you need to copy students whose java scores exceed 90 into the java_result table. The copied fields are name, java .

    Before performing the above operations, we need to create a student table and prepare relevant data:

    create table student (
        id int primary key,
        name varchar(20),
        sex varchar(1),
        java float(5, 2)
    );
    insert into student value 
        (1, '张三', '男', 92.1),
        (2, '小红', '女', 88.2),
        (3, '赵六', '男', 83.4),
        (4, '王五', '男', 93.3),
        (5, '小美', '女', 96.0);
    Copy after login

    After having the student table, we need to query the results of the two fields name and java Copy it to the java_result table. Here we note that the number and column type of the temporary table that requires the query results must match java_result, so next we will create the java_result table:

    create table java_result (
        name varchar(20),
        java float(5, 2)
    );
    Copy after login

    After creating the java_result table, you need to query the name java fields in the student table, and java > 90. Insert the query results that meet the above conditions into the java_result table! :

    insert into java_result select name, java from student where java > 90;
    -- Query OK, 3 rows affected (0.00 sec)
    -- Records: 3  Duplicates: 0  Warnings: 0
    Copy after login
    select * from java_result;
    +--------+-------+
    | name   | java  |
    +--------+-------+
    | 张三   | 92.10 |
    | 王五   | 93.30 |
    | 小美   | 96.00 |
    +--------+-------+
    -- 3 rows in set (0.00 sec)
    Copy after login

    In this way, we find that all the data whose name and java fields in the student table satisfy > 90 have been inserted successfully!

    3. Aggregation query

    The queries with expressions we have come across before are all operations between columns to see which column satisfies this condition.

    The aggregation query we are going to introduce now is based on operations between rows!

    3.1 Aggregation function

    To perform aggregate query, aggregate function must be used. The functions introduced below are all a set of functions built into SQL. Let us first briefly understand them

    FunctionExplanation
    COUNT([DISTINCT] expr)Return the number of queried data
    SUM([DISTINCT] expr )Returns the sum of the queried data, which is not a meaningless number
    AVG([DISTINCT] expr)Returns the queried data The average value, not a numerical meaningless value
    MAX([DISTINCT] expr)Returns the maximum value of the queried data, not a numerical meaningless value
    MIN([DISTINCT] expr)Returns the minimum value of the queried data. It is meaningless if it is not a number

    下面我们就来演示一下上述的聚合函数的简单使用,在使用之前,我们需要有一张表,并且有相应的数据:

    select * from student;
    +----+--------+------+-------+
    | id | name   | sex  | java  |
    +----+--------+------+-------+
    |  1 | 张三   | 男   | 92.10 |
    |  2 | 小红   | 女   | 88.20 |
    |  3 | 赵六   | 男   | 83.40 |
    |  4 | 王五   | 男   | 93.30 |
    |  5 | 小美   | 女   | 96.00 |
    |  6 | 李四   | 男   |  NULL |
    +----+--------+------+-------+
    -- 6 rows in set (0.00 sec)
    Copy after login

    下面我们就针对上述这张表,来使用下上述的聚合函数。

    3.1.1 count

    ● 求出 student 表中有多少同学

    select count(*) from student;
    +----------+
    | count(*) |
    +----------+
    |        6 |
    +----------+
    -- 1 row in set (0.00 sec)
    Copy after login

    这个操作就相当于先进行 select * ,然后针对返回的结果,在进行 count 运算,求结果集合的行数. 注意:此处如果有一列的数据全是 null,也会算进去!(因为是针对 *)

    此处这里的 count() 括号中,不一定写 *,可以写成任意的列明/表达式,所以我们可以针对 name 来统计人数:

    select count(name) from student;
    +-------------+
    | count(name) |
    +-------------+
    |           6 |
    +-------------+
    -- 1 row in set (0.00 sec)
    Copy after login

    ● 统计有多少人有 java 考试成绩

    select count(java) from student;
    +-------------+
    | count(java) |
    +-------------+
    |           5 |
    +-------------+
    -- 1 row in set (0.00 sec)
    Copy after login

    这里我们看到了,由于 count 是针对 java 字段进行统计,而 李四 那一条数据中,java 为 null,前面我们学习过,null 与任何值计算都是 null,所以统计的时候,就把 null 给去掉了。

    ● 统计 java 成绩大于90分的人数

    select count(java) from student where java > 90;
    +-------------+
    | count(java) |
    +-------------+
    |           3 |
    +-------------+
    -- 1 row in set (0.00 sec)
    Copy after login

    这里我们要弄清楚,count() 这个括号中,是针对你要针对的那一列,针对不同列,不同的条件,就会有不同的结果,对于 count 的演示就到这里。

    注意:count 和 () 之间不能有空格,必须紧挨着,在 Java 中函数名和() 之间是可以有空格的,但很少人会这样写。

    3.1.2 sum

    这个聚合函数,就是把指定列的所有行进行相加得到的结果,要求这个列得是数字,不能是字符串/日期。

    ● 求出学生表中 java 考试分数总和

    select sum(java) from student;
    +-----------+
    | sum(java) |
    +-----------+
    |    453.00 |
    +-----------+
    -- 1 row in set (0.01 sec)
    Copy after login

    虽然我们表中有 java 字段这列中有 null 值,前面了解到 null 与任何值运算都是 null,但是这里的 sum 函数会避免这种情况发生。

    当然在后面也可也带上 where 条件,这里就不做过多演示了。

    3.1.3 avg

    ● 求班级中 java 的平均分

    select avg(java) from student;
    +-----------+
    | avg(java) |
    +-----------+
    | 90.600000 |
    +-----------+
    -- 1 row in set (0.00 sec)
    Copy after login

    当前只是针对某一列进行平均运算,如果有两门课程,求每个学生总分的平均分呢?

    select avg(java + python) from student;
    Copy after login

    这里每次查询结果都只有一列,能否把两个聚合函数一起使用呢?

    select sum(java), avg(java) as '平均分' from student;
    +-----------+-----------+
    | sum(java) | 平均分    |
    +-----------+-----------+
    |    453.00 | 90.600000 |
    +-----------+-----------+
    -- 1 row in set (0.00 sec)
    Copy after login

    这里我们能发现一个细节,使用聚合函数查询,字段也是可以取别名的。

    3.1.4 max 和 min

    ● 求出 java 考试分数的最高分和最低分

    select max(java) as '最高分', min(java) as '最低分' from student;
    +-----------+-----------+
    | 最高分    | 最低分    |
    +-----------+-----------+
    |     96.00 |     83.40 |
    +-----------+-----------+
    -- 1 row in set (0.00 sec)
    Copy after login

    上述就是聚合函数最基础的用法了, 但是在实际中也可能会有更复杂的情况,比如需要按照某某进行分组查询,这就需要搭配 GROUP BY 字句了。

    4、GROUP BY 子句

    select 中使用 group by 自居可以对指定列进行分组查询,但是需要满足指定分组的字段必须是 "分组依据字段",其他字段若想出现在 select 中,则必须包含在聚合函数中。

    这里我们构造出一张薪水表 salary:

    create table salary (
        id int primary key,
        name varchar(20),
        role varchar(20),
        income int 
    );
    insert into salary value 
        (1, '麻花疼', '老板', 5000000),
        (2, '篮球哥', '程序猿', 3000),
        (3, '歪嘴猴', '经理', 20000),
        (4, '多嘴鸟', '经理', 25000),
        (5, '雷小君', '老板', 3000000),
        (6, '阿紫姐', '程序猿', 5000);
    Copy after login

    像上述的情况,如果要查平均工资,那公平吗???

    select avg(income) from salary;
    +--------------+
    | avg(income)  |
    +--------------+
    | 1342166.6667 |
    +--------------+
    -- 1 row in set (0.00 sec)
    Copy after login

    那篮球哥的月薪连平均下来的零头都不到,所以这样去求平均工资是毫无意义的,真正有意义的是啥呢?求老板这个职位的平均工资,以及经理这个职位的平均工资,及程序猿这个职位的平均工资,通俗来说,就是按照 role 这个字段进行分组。每一组求平均工资:

    select role, avg(income) from salary group by role;
    +-----------+--------------+
    | role      | avg(income)  |
    +-----------+--------------+
    | 程序猿    |    4000.0000 |
    | 经理      |   22500.0000 |
    | 老板      | 4000000.0000 |
    +-----------+--------------+
    -- 3 rows in set (0.00 sec)
    Copy after login

    此句可以重写为:这是将role列中值相同的行分为一组,然后按组计算平均值,也是针对每个组分别计算。

    在 MySQL 中,这里得到的查询结果临时表,如果没有 order by 指定列排序,这里的顺序是不可预期的,当然也可以手动指定排序,比如最终结果按照平均工资降序排序:

    select role, avg(income) from salary group by role order by avg(income) desc;
    +-----------+--------------+
    | role      | avg(income)  |
    +-----------+--------------+
    | 老板      | 4000000.0000 |
    | 经理      |   22500.0000 |
    | 程序猿    |    4000.0000 |
    +-----------+--------------+
    -- 3 rows in set (0.00 sec)
    Copy after login

    如果不带聚合函数的普通查询,能否可行呢?这里如果你没有修改任何配置文件,是不可行的,记住千万不能把前面的 order by 与 group by 弄混!

    5、HAVING 关键字

    分组查询也是可以指定条件的,具体三种情况:

    • 先筛选,再分组(where)

    • 先分组,再筛选(having)

    • 分组前分组后都指定条件筛选(where 和 having 结合使用)

    如何理解上述三条的含义呢? 这里我们举几个例子就很好理解了:

    ● 篮球哥月薪 3000 实在是太低了,简直给程序猿岗位拖后腿,干脆求平均工资时去掉篮球哥的月薪数据。

    select role, avg(income) from salary where name != '篮球哥' group by role;
    +-----------+--------------+
    | role      | avg(income)  |
    +-----------+--------------+
    | 程序猿    |    5000.0000 |
    | 经理      |   22500.0000 |
    | 老板      | 4000000.0000 |
    +-----------+--------------+
    -- 3 rows in set (0.00 sec)
    Copy after login

    这样求出来的平均值就不包含篮球哥的月薪数据了,这就是先筛选,再分组。

    ● 还是查询每个岗位的平均工资,但是除去平均月薪在 10w 以上的岗位,不能让篮球哥眼红!

    select role, avg(income) from salary group by role having avg(income) < 100000;
    +-----------+-------------+
    | role      | avg(income) |
    +-----------+-------------+
    | 程序猿    |   4000.0000 |
    | 经理      |  22500.0000 |
    +-----------+-------------+
    -- 2 rows in set (0.00 sec)
    Copy after login

    这样一来就只保留了平均月薪小于 10w 的岗位了,很明显这个平均值是在分组之后才算出来的,这也就是先分组,再筛选。

    这里 having 也能加上逻辑运算符,具体感兴趣的小伙伴可以自行下来尝试一下,好比如你想要拿好 offer,就得技术过关,还能加班!至于第三种分组前后都需要筛选,就是把上述俩例子结合起来,这里就不多赘述了!

    The above is the detailed content of How to use MySQL aggregate query method. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Chat Commands and How to Use Them
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

    MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

    How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

    You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

    How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

    Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

    MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

    MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

    How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

    You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

    How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

    Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

    How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

    Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

    MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

    MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

    See all articles