MySQL --- aggregate query and union query

Aggregation query:

All the followingaggregation query example operations are based on this table:

aggregate function

Aggregation functions are operations between rows.

count()

select count(列名) from 表名;

Counts the number of rows of this column in the table, butnull values ​​will not be counted,but if written as count(*) Then null will also be counted (Even if the entire row is null, it will be counted)

For example, we temporarily use this table for demonstration:

This table has one row that is all empty

sum()

select sum(列名) from 表名;

It can only be used for numeric types, and it can also be added to multiple columns to get a total;

Sum the values ​​of the math column in this table.

Note: null values ​​will be excluded and not calculated.

avg()

select avg(column name) from table name;

Average this column in the table.

Can only be used for numeric types

Note: null values ​​will be excluded and not calculated.

max()

select max(列名) from 表名;

Find the maximum value of this column in the table.

Can only be used for numeric types

Note: null values ​​will be excluded and not calculated.

min()

select min(column name) from table name;

Find the minimum value of this column in the table.

Can only be used for numeric types

Note: null values ​​will be excluded and not calculated.

group by

group by can group data.

The general usage method is to first use group by to group, and then use the aggregate function to perform aggregate queries.

select * from 表名 group by 列名;

group by will group the s with the same value in the column into one group. Because does notuse order by for sorting operationso < a i=7>MySQL does not guarantee the order of query results.

At this time, we add another row of data to the original table.

After adding this piece of data, the table becomes:

At this time, perform the group query operation:

Because no aggregation function is used at this time, Zhang San’s row just selects a representative from Zhang San’s group.

Ifadd aggregation function, you can clearly see that the aggregation function will be Operate for units.

At this time, if we do not want a certain row (certain rows) to participate in the grouping, we can add where before group by to perform conditional filtering.

select * from table name where condition group by column name;

Example: At this time we do not want the null value and the rows with math=100 to participate in the grouping 

Note: where condition must precede group by

An error will be reported later.

having

In fact, having is almost the same as where and it is a conditional filtering statement.

After grouping with the group by clause, when you need to conditionally filter the grouped results, you cannot use the where statement, but need to use having

Having is used to filter the data after grouping.

Example: At this time, we do not want the null value and the row with grade=100 to participate in the grouping 

The statement execution sequence at this time is:

So the conditions in the having clause must correspond to those in select

Note: having must be written after the group by clause.


Union query:

In actual development, data often comes from different tables, so joint queries of multiple tables are required.

The key to multi-table query is to take the Cartesian product of data from multiple tables:

Let me first briefly introduce the Cartesian product. Suppose we now have the following two tables. The Cartesian product of their data is to combine each row of data in the first table with each row of data in the second table.​ 

After the Cartesian product of these two tables, it becomes a new table with 9 rows and 4 columns, as shown in the figure.

This is the result of taking the Cartesian product of two tables.

However, we can easily find that there is a lot of so-called illegal data.

From the picture, we can see that only three pieces of data are legal, and when we perform database operations, we must not allow so much illegal data to appear in the query results. At this time, we can find the associated attributes of the two tables To filter through the where condition, for example, the above table can be filtered by setting the classes of the two tables to be the same.

inner join

Inner join is actually to find the intersection in multiple tables (or in other words, the link condition of each piece of data in the final query result exists in the original multiple tables).

For better demonstration here, I first created three tables

There are only these two statements for inner joins: 

select field from table 1  [inner] join table 2 on connection conditions and other conditions;

select field from table 1, table 2  where connection conditions and other conditions;

The above formats may seem complicated, but there is no need to remember them at all. Just follow the steps below and practice a few times to master them.

Inner joins are essentially divided into the following steps:

Example: Check ADLINK’s scores in various subjects.

Step 1: Find the Cartesian product first

select * from Table 1, Table 2,...

Through the above statement, the Cartesian product of multiple tables can be found.

At this time, a total of 27 pieces of data were output.

Step 2: Add connection conditions to filter out legitimate information

It is recommended to use the writing method of table name dotted with column name when writing the conditions in where because the table may have the same Column name.

Step 3: Further add conditions based on needs and filter the results;

The condition that needs to be added here is that the name is Linghua.

Step 4: Streamline the columns and filter out the columns that need to be displayed

At this point, if you compare the expressions at the beginning, you will find that they are exactly the same. Basically, these four steps can be applied to all inner connections.

select field from table 1  [inner] join table 2 on connection conditions and other conditions;

select field from table 1, table 2  where connection conditions and other conditions;

Note: [inner] join in the first statement inner  can be omitted. is enclosed in [] because

outer join

If there is a corresponding relationship between the information in multiple tables, the results of the inner join and the outer join will be the same. If the data in the table does not correspond to the inner and outer join, there will be differences.

Outer joins are divided into left outer joins and right outer joins.

Example: There are two tables below with data that do not correspond exactly to each other.

left outer join

select field from table 1 left join table 2 on connection condition and other conditions;

This expression is the same as the inner join except left.

The left outer join is based on the left table. If the left table has one and the right table does not, then use null instead.

right outer join

select field from Table 1  right join Table 2 on join conditions and other conditions;

This expression is the same as a left outer join except for right.

The right outer join is based on the right table. If the right table exists and the left table does not, then is replaced by null.

self-connection

Self-joining means taking the Cartesian product of itself and itself, which is the same as the problem-solving method of inner joining.

Note: In self-joining, the table must be aliased otherwise an error will be reported.

Advantages of self-joining: The relationship between rows can be converted into columns and columns.

Example: In the above table, search for people whose C++ scores are greater than those of microcontrollers.

subquery

Subqueries are essentially "matryoshka dolls" that nest multiple simple SQL statements into one complex SQL statement.

 

Example: Find out what grade the student named Keli is in.

1. First find out Keli’s class number

2. Find the class name in the class table according to the class number

The subquery is to nest these two SQL statements

merge query

Merged queries are implemented through union

In MySQl, you can use union to combine the query results of multiple tables into one table.

Note: The number of columns and data types between the corresponding columns of the two merged results must be the same (the column naming system will automatically follow the column names of the first table), and union will remove duplicates in the results.

For example, querying the id and name results in these two tables returns one table;

There is only one student with id = 1 in this result.

If you do not want to deduplicate the results, you can use union all 

If you use union on the same table, then itsresult will be the same as using or a>.

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/133903514