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);
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) );
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
select * from java_result; +--------+-------+ | name | java | +--------+-------+ | 张三 | 92.10 | | 王五 | 93.30 | | 小美 | 96.00 | +--------+-------+ -- 3 rows in set (0.00 sec)
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
Function | Explanation |
---|---|
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)
下面我们就针对上述这张表,来使用下上述的聚合函数。
3.1.1 count
● 求出 student 表中有多少同学
select count(*) from student; +----------+ | count(*) | +----------+ | 6 | +----------+ -- 1 row in set (0.00 sec)
这个操作就相当于先进行 select * ,然后针对返回的结果,在进行 count 运算,求结果集合的行数. 注意:此处如果有一列的数据全是 null,也会算进去!(因为是针对 *)
此处这里的 count() 括号中,不一定写 *,可以写成任意的列明/表达式,所以我们可以针对 name 来统计人数:
select count(name) from student; +-------------+ | count(name) | +-------------+ | 6 | +-------------+ -- 1 row in set (0.00 sec)
● 统计有多少人有 java 考试成绩
select count(java) from student; +-------------+ | count(java) | +-------------+ | 5 | +-------------+ -- 1 row in set (0.00 sec)
这里我们看到了,由于 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)
这里我们要弄清楚,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)
虽然我们表中有 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)
当前只是针对某一列进行平均运算,如果有两门课程,求每个学生总分的平均分呢?
select avg(java + python) from student;
这里每次查询结果都只有一列,能否把两个聚合函数一起使用呢?
select sum(java), avg(java) as '平均分' from student; +-----------+-----------+ | sum(java) | 平均分 | +-----------+-----------+ | 453.00 | 90.600000 | +-----------+-----------+ -- 1 row in set (0.00 sec)
这里我们能发现一个细节,使用聚合函数查询,字段也是可以取别名的。
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)
上述就是聚合函数最基础的用法了, 但是在实际中也可能会有更复杂的情况,比如需要按照某某进行分组查询,这就需要搭配 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);
像上述的情况,如果要查平均工资,那公平吗???
select avg(income) from salary; +--------------+ | avg(income) | +--------------+ | 1342166.6667 | +--------------+ -- 1 row in set (0.00 sec)
那篮球哥的月薪连平均下来的零头都不到,所以这样去求平均工资是毫无意义的,真正有意义的是啥呢?求老板这个职位的平均工资,以及经理这个职位的平均工资,及程序猿这个职位的平均工资,通俗来说,就是按照 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)
此句可以重写为:这是将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)
如果不带聚合函数的普通查询,能否可行呢?这里如果你没有修改任何配置文件,是不可行的,记住千万不能把前面的 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)
这样求出来的平均值就不包含篮球哥的月薪数据了,这就是先筛选,再分组。
● 还是查询每个岗位的平均工资,但是除去平均月薪在 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)
这样一来就只保留了平均月薪小于 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!

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),