mysql expression or function by grouping, a plurality of packet fields, sorting

Press the expression or function groups:

Case #: The number of employees grouped according to the length of the employee's name, query the number of employees in each group were screened greater than five

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

By grouping multiple fields:
# Example: query the average wage of employees in each department in each trade

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

Sort
# Case: query each department department number each trade is not null employees average salary> 10000, and the display according to the level of the average wage

SELECT
    AVG(salary),department_id,job_id
FROM
    employees
WHERE
    department_id IS NOT NULL
GROUP BY
    department_id,job_id
HAVING
    AVG(salary)>10000
ORDER BY
    AVG(salary) DESC;

Guess you like

Origin blog.51cto.com/14437184/2436653