1. Grouping: GROUP BY statements
Group by must follow the conditions in the WHERE clause before order by (if any), as follows:
Select Column1, Column2 from table1, table2 where Conditionsgroup by Column1, Column2order by Column1, Column2
1) The selected field (the Select field in the query, such as COLUMN1,COLUMN2) can and must appear in the GROUP BY clause, except the summary function , if the field is not found in the Select, cannot be used with the GROUP BY clause. This is logical-how to group the data if it is not displayed.
2) The order of the fields in the GROUP BY clause is not the same as in the SELECT clause, and the groupings are equal, regardless of the order.
2.HAVING clause
The HAVING clause is used in conjunction with select and group by to tell Group by which groupings should be included in the output. Having a role for group by is equivalent to where for select. Therefore, using the HAVING clause allows the result to include or remove the entire set of data.
Having to follow group by before order by
MySQL-group BY, having