|
First of all, a simple explanation of group by: Group by generally makes sense to use it with aggregation functions, such as count sum avg, etc., using two elements of group by: (1) The field that appears after select is either in the aggregation function or in the group by. (2) To filter the results, you can use where first and then group by or group by first and then having Let's take a look at the analysis of multiple conditions of group by: Enter the following statement in the SQL queryer create table test
( a varchar(20), b varchar(20), c varchar(20)
) insert into test values(1,'a','a') insert into test values(1,'a','a') insert into test values(1,'a','a') insert into test values(1,'a','a') insert into test values(1,'a','b') insert into test values(1,'b','b') insert into test values(1,'b','b') insert into test values(1,'b','b') First query select * from test; The result is shown below: In the results, according to column b: 5 a and 3 b. According to column C: 4 A and 4 B.
The second group by column b code is as follows select count(a),b from test group by b
The third group according to column C is as follows select count(a),c from test group by c
The fourth time is grouped according to the two conditions of b and c select count(a),b,c from test group by b,c
The fifth time is grouped in the order of c and b select count(a),b,c from test group by c,b
It can be seen that group by two conditions work in the working process: First, the values of column b of the first condition are grouped into the first group: 1-5, and the second group is 6-8, and then the two existing groupings are grouped with the values of column c of condition 2, and it is found that the first group can be divided into two groups 1-4,5
|