MySQL:多表连接查询(2)

3. sql99标准

语法

select 查询列表
from 表1 别名 【连接类型】
join 表2 别名
on 连接条件(内连接 inner,左外连接 left outer,右外连接 right outer,全外连接 full outer,交叉连接 cross)
【where 筛选条件】
【group by 分组条件】
【having 筛选条件】
【order by 排序列表】


内连接 - 等值连接

  • 案例:查询员工名和对应的部门名
SELECT 
  `last_name`,
  `department_name` 
FROM
  `departments` AS d 
  INNER JOIN `employees` AS e 
    ON e.`department_id` = d.`department_id` ;
  • 案例:查询名字中包含e的员工名和工种名
SELECT 
  `last_name`,
  `job_title` 
FROM
  `jobs` AS j 
  INNER JOIN `employees` AS e 
    ON e.`job_id` = j.`job_id` 
WHERE `last_name` LIKE "%e%" ;
  • 案例:查询部门个数大于3的城市名和部门个数
SELECT 
  `city`,
  COUNT(*) 
FROM
  `departments` AS d 
  INNER JOIN `locations` AS l 
    ON d.`location_id` = l.`location_id` 
GROUP BY `city` 
HAVING COUNT(*) > 3 ;
  • 案例:查询员工个数大于3的部门名和员工个数,并按照个数降序排序
SELECT 
  `department_name`,
  COUNT(*) 
FROM
  `departments` AS d 
  INNER JOIN `employees` AS e 
    ON d.`department_id` = e.`department_id` 
GROUP BY `department_name` 
HAVING COUNT(*) > 3 
ORDER BY COUNT(*) DESC 
  • 案例:查询员工名,部门名,工种名,并按部门名降序
SELECT 
  `last_name`,
  `department_name`,
  `job_title` 
FROM
  `employees` AS e 
  INNER JOIN `departments` AS d 
    ON d.`department_id` = e.`department_id` 
  INNER JOIN `jobs` AS j 
    ON e.`job_id` = j.`job_id` 
ORDER BY `department_name` DESC ;

总结:

  1. 可以添加排序,分组,筛选
  2. inner可以省略
  3. 筛选条件放在where后面,连接条件放在on后面,提高分离性,便于阅读
  4. inner join连接和sql92语法中的等值连接效果是一样的,都是查询多表的交集

内连接 - 非等值连接

  • 案例:查询员工的工资和工资级别
SELECT 
  `last_name`,
  `salary`,
  `grade_level` 
FROM
  `employees` AS e 
  INNER JOIN `job_grades` AS jd 
    ON `salary` BETWEEN `lowest_sal` 
    AND `highest_sal` 
  • 案例:查询工资级别的员工个数大于20的,按照工资级别降序系列
SELECT 
  `grade_level`,
  COUNT(*) 
FROM
  `employees` AS e 
  INNER JOIN `job_grades` AS jd 
    ON `salary` BETWEEN `lowest_sal` 
    AND `highest_sal` 
GROUP BY `grade_level` 
HAVING COUNT(*) > 20 
ORDER BY `grade_level` DESC 

内连接 - 自连接

  • 案例:查询员工名以及上级的名称
SELECT 
  e.`employee_id`,
  e.`last_name`,
  e.`manager_id`,
  m.`last_name` 
FROM
  `employees` AS e 
  INNER JOIN `employees` AS m 
    ON e.`manager_id` = m.`employee_id` 
WHERE e.`last_name` LIKE "%k%" 

外连接

  • 案例:查询男朋友不在男神表的女神名
-- 左外连接
SELECT 
  g.`name` 
FROM
  `beauty` AS g 
  LEFT OUTER JOIN `boys` AS b 
    ON g.`boyfriend_id` = b.`id` 
WHERE b.`id` IS NULL 
-- 右外连接
SELECT 
  g.`name` 
FROM
  `boys` AS b 
  RIGHT OUTER JOIN `beauty` AS g 
    ON g.`boyfriend_id` = b.`id` 
WHERE b.`id` IS NULL 
  • 案例:查询没有员工的部门
SELECT DISTINCT 
  d.`department_id` 
FROM
  `departments` AS d 
  LEFT OUTER JOIN `employees` AS e 
    ON d.`department_id` = e.`department_id` 
WHERE e.`employee_id` IS NULL 

总结:

  1. 用于查询一个表中有,另一个表中没有的数据
  2. 左外连接中,左侧为主表;右外连接,右侧为主表;左外和右外交换表的顺序可以实现同样的效果
  3. 外连接的查询结果为主表中的所有数据,如果从表中有与之匹配的,就显示匹配记录,否则显示null
  4. 全外连接会将左外连接和右外连接的结果组合在一起

交叉连接

  • 含义即为两表的笛卡尔乘积
SELECT 
  * 
FROM
  `beauty` 
  CROSS JOIN `boys` 

你可能感兴趣的:(MySQL:多表连接查询(2))