[MySQL] Advanced query - aggregation query and joint query

1 Introduction

The article mainly revolves around the following three questions:

  1. The role of group by
  2. The difference between where and having
  3. What are the joins of tables, and what are their functions?

2. Table design

When creating a data table, we usually find "entities" according to the needs, sort out the relationship between "entities", and create them. There
may be the following relationships between "entities": 1. It doesn't matter 2. One-to-one 3. One-to-many 4. Many-to-many.
It doesn't matter. It should be the best understanding. It is a single table and does not involve other tables.

2.1 One to one

One-to-one relationship is very common in life. For example, each student has his own student number, and each student number corresponds to only one student. Similar to this situation, it is a one-to-one relationship. At this
time You can create two tables, one is the student table, and the other is the student number table. The student number in the student table can be associated with the student number in the student number table.

2.2 One-to-many

When students are in school, there will be a class. But a class can have multiple students. This is a one-to-many relationship.

2.3 Many-to-many

For example, when we study courses, we can choose multiple courses to study, and courses can also be selected by multiple students. This is the many-to-many relationship. Many-to-many
relationships, when creating tables, can be used A "relation table" links two entities.

If the scene is very complicated when designing the database table, you can use the ER diagram to help us better create the database table.

3. Put the query results into another table

insert into2 select 字段 from1;
# 表2 是要存放查询结果的表
# 表1 是要查询的表
# 查询结果的列要和表2的列相匹配!
# 也可以将查询结果存放在表2的指定列中

Example:
There are three pieces of data in the "student1" table below.
insert image description here
Next, we put the query results into a new "student2" table
insert image description here

4. Aggregation query

Aggregate queries can perform operations between "rows" and "rows"

4.1 Aggregate functions

function illustrate
COUNT([DISTINCT] expr) Returns the number of queried data
SUM([DISTINCT] expr) Returns the total number of queried data
AVG([DISTINCT] expr) Returns the average value of the queried data
MAX([DISTINCT] expr) Returns the maximum value of the queried data
MIN([DISTINCT] expr) Returns the minimum value of the queried data
  • The count function can perform calculations on rows as well as columns.

Example:
insert image description here
Here is a student table, which contains the student's id, name, and grades in English, mathematics and English.
insert image description here

  • sum, avg, max and min are all valid only for numbers, if they are not numbers, they are meaningless

The usage of the sum function is similar to the count function. However, it can only calculate "columns". If there is "null" in this column, it will not participate in the calculation
.

4.2 GROUP BY

The GROUP BY statement is used in combination with aggregate functions to group the result set by one or more columns.

For example:
insert image description here
I have some data in my student table. Now I want to group the Chinese scores.
After using group by, we can see that this has been grouped by Chinese scores.
insert image description here
There is one score of 57, and two scores of 69. ,82 has three

It should be noted that when querying and grouping, only the grouped column can be queried, and other columns must be queried with aggregation functions

4.3 HAVING

Grouping queries can also specify conditions, here only when the conditions can be specified before the grouping can also be specified after the grouping. 分组前进行筛选使用的是where 分组后进行筛选使用的则是havingThe reason for adding the HAVING clause in SQL is because the WHERE keyword cannot be used with the aggregate function.
Group after removing the Chinese score with id=1 After grouping,
insert image description here
filter out the number of students whose Chinese score is > 60.
insert image description here
The where keyword cannot be used with the aggregate function, but it can be used with having

5. Joint query (multi-table query)

Joint query is generally the process of querying after establishing a connection between multiple tables. In fact, it is the process of calculating the "Cartesian product".
But when the table is large, if the joint query is performed, the efficiency will be particularly low. Because of the "Cartesian product "It is a simple permutation and combination. Some data are "reasonable" and some data are "unreasonable". So we have to filter out the "valid" data. Therefore, joint queries usually need to add connection conditions and other filter conditions

5.1 Inner joins

The inner connection gets the data that exists in both tables. There are
two ways of writing:

select 字段 from1,2;
select 字段 from1 join2 on 条件;

For example:
Student table:
insert image description here
Class table:
insert image description here
It is required to query the name of the class of "Wang Wu".
This involves two tables, and a joint (multi-table) query is required.

1. Cartesian product first
insert image description here
is actually the arrangement and combination of student table and class table, which contains a lot of invalid data.
2. Add connection conditions
insert image description here
Note that the condition writing here should be 表名.字段名, because it involves multiple tables, in multiple tables The field names may be the same, so you need to use 表名.字段to indicate the field in which table. Of course如果这个列名是唯一的,也可以不加 表名.

insert image description here
There are still too many results just now, we can also add specified columns to query, and we also need to use 表名.字段the form to query

The same effect can also be achieved by using join on , which is the same as the example just now.
Use join on to complete
1. First perform Cartesian product
insert image description here
2. Add conditions
insert image description here
Directly write join or inner join is an inner connection
join on不仅可以实现内连接,还可以实现外连接

5.2 Outer joins

The outer connection is divided into left outer connection (left join) and right outer connection (right join). It
is still the student table just now:
insert image description here
but there are two more data in the class table.
insert image description here
The result obtained after Cartesian product
insert image description here
is the result of the inner connection:
insert image description here
This is the result of right join.
insert image description here
Right join will get all the data in the right table. Even if the value on the left is NULL, it will be displayed.
Left join is similar, and will get all the data in the left table. Even if the value on the right is NULL, it will be displayed.

如果两张表中的数据,在对方表中都有,那么此时内外连接是没有区别的,如果两张表中的数据只有一部分在对方的表中,内连接就是获取两张表的"交集",如果是外连接,那么获取到的值就是一侧表的全部记录.

Another kind of connection is "full outer connection", but it is not supported in MySQL

5.3 Self-join

Self-connection is to perform Cartesian product with itself.
In conditional query, it is just a comparison between "column" and "column", but in some places, the comparison between "row" and "row" needs to be used. Connect, convert "row" to "column" and compare

For example, here is a score table.
insert image description here
If you want to query the names of students with higher math scores than Chinese scores, you need to use self-join. Because if you compare, you can see the comparison between "line" and "line". If it is
insert image description here
directly If you connect, you will get an error.
Not unique table/alias: 'grades': 这句话告诉我们不是唯一的表,但是可以起别名

insert image description here
Through the method of aliasing, the self-connection is successfully performed.
Add the connection conditions, and first filter out a part of the records. At this time, we can see that the Chinese scores and math scores are in two columns.
insert image description here
Complete the conditions and we can get the results we want. up
insert image description here

5.4 Subqueries

The essence of a subquery is to combine multiple query words into one SQL statement, for example, to query again on the temporary table obtained by the query

For example: In the class table, find the students who are in the same class as "Zhang San"
insert image description here
and find the class id of "Zhang San", which must be known to everyone.
insert image description here
We will continue to participate in the query with the result obtained:
insert image description here
at this time, we will get the classmate "Li Si" This record of因为这里的班级id就只有一个,所以后面使用的是 = ,但是如果这个的临时表数据有多条,就可以使用 in 来完成

5.5 Combined queries

Merge query is to combine the result sets of two queries together.使用的是union 和union all 这两个关键字

  • union: If there is duplicate data, it will be deduplicated
  • union all: If there is duplicate data, it will not be deduplicated

Still the score table just now
insert image description here
If we want to query the names of people whose math scores are > 90 and English scores < 60, we can use the combined queryinsert image description here

6. Summary

In SQL statements, the operation of querying data is still somewhat difficult compared with other operation statements, mainly involving some operations such as multi-table query. For some keywords involved in it, the connection type must be mastered

Thank you for watching! I hope this article can help you!
Column: "Speedthrough MySQL" is constantly being updated, welcome to subscribe!
"Wish to encourage you and make progress hand in hand!"
insert image description here

Guess you like

Origin blog.csdn.net/m0_63463510/article/details/129956597