MySQL Query -GROUP BY grouping

Grouping query syntax
select grouping functions, column
from table
[where] Filters - where positions can be placed before or after the group by, the two have different meanings
list group by grouping
[having] Filters
[order by] - If you use to sort, order by the statement in the final basic

where the use of features: 1,
a data source position key
of the packet filter derived 'from' group by the front of the table where
the packet from the packet filtering function calculated result set group by HAVING

2, group by clause supports a single field, (in between fields, separated) a plurality of fields, the expression or function

SELECT MAX(salary),job_id
FROM employees
GROUP BY job_id;

SELECT COUNT(*),location_id
FROM departments
GROUP BY location_id;

Add Filters

SELECT AVG(salary),department_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id;

SELECT MAX(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;

Add a complex filtering condition

SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id;

SELECT job_id,MAX(salary),commission_pct
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;

Press the expression or function grouping

SELECT COUNT(),LENGTH(last_name) len_name
FROM employees
GROUP BY LENGTH(last_name)
HAVING COUNT(
)>5;

By a plurality of packet fields

SELECT AVG(salary),job_id,department_id
FROM employees
GROUP BY job_id,department_id;

Add sorting

SELECT AVG(salary),job_id,department_id
FROM employees
GROUP BY job_id,department_id
ORDER BY AVG(salary) DESC;

Guess you like

Origin www.cnblogs.com/bjhs/p/12663592.html