mysql multi-table queries cases

Case #: select city employees working in Toronto

SELECT
    last_name,job_id,e.department_id,department_name
FROM
    employees AS e,departments AS d,locations AS l
WHERE
    e.`department_id`=d.`department_id`
AND
    d.`location_id`=l.`location_id`
AND
    l.city='Toronto'

# Case; query the number of departments at the national number for each country is greater than 2

SELECT
    country_id ,COUNT(*)
FROM
    locations AS l,departments AS d
WHERE
    l.`location_id`=d.`location_id`

GROUP BY
    country_id
HAVING
    COUNT(*) >2;

Guess you like

Origin blog.51cto.com/14437184/2437877