Reference materials: 1, SQL statement: Group By summary
1. There are multiple fields behind group by
If there are multiple fields after group by, they are similar to the multiple fields after order by. For example: group by a, b; merge multiple pieces of data shared by fields a and b, and different data will not be merged. Specific examples are as follows:
2. Having behind group by
Having after group by is to filter and screen the grouped fields again, generally using aggregate functions. The following example: (group field a, and the number before grouping must be greater than 1)
select a,
count(1),
max(b)
from f
where a like 'X%'
group by a
having count(a) > 1
Or you can filter aggregate functions for non-grouping fields
3. The HAVING clause can contain one or more predicates connected by AND and OR
Fourth, after using HAVING sentence, use count (*) after select is different
SELECT COUNT(*),Ssex FROM Student GROUP BY Ssex;
Five, the keyword ALL after group by
The keyword All after the group by, if it is filtered by the where condition, the group by will be performed. Then the keyword All is to display all the data that does not meet the conditions.
Six, the keywords CUBE and ROLLUP after group by
The first thing to note is that the Group By All statement cannot be used with the CUBE and ROLLUP keywords.
7. Precautions for using field aliases
Because the execution order of mysql is as follows:
from
on
join
where
group by
select
having
order by
limit
From the above, we can see that the field alias after select can only be used after the select is executed.