文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

【MySQL】多表查询(JOIN / UNION)

2023-09-04 06:01

关注

文章目录

多表查询

1. 笛卡尔积错误

#查询员工姓名和所在部门名称SELECT last_name,department_name FROM employees, departments;SELECT last_name,department_name FROM employees CROSS JOIN departments;SELECT last_name,department_name FROM employees INNER JOIN departments;SELECT last_name,department_name FROM employees JOIN departments;# 结果会将last_name所有值和department_name所有值做排列组合
SELECT table1.column, table2.columnFROM table1, table2WHERE table1.column1 = table2.column2; #连接条件

2. 等值连接 WHERE

# 表名前缀SELECT employees.employee_id, employees.last_name, employees.department_id, departments.location_idFROM employees, departmentsWHERE employees.department_id = departments.department_id;# 别名SELECT e.last_name, d.department_id, e.department_idFROM employees e, departments AS d WHERE e.department_id = d.department_id;# 连接多个表,连接 n个表,至少需要n-1个连接条件。SELECT e.employee_id, e.last_name, e.department_id, d.department_name, d.location_id, l.location_id, l.cityFROM employees e, departments d, locations lWHERE e.department_id = d.department_id AND d.location_id = l.location_idORDER BY e.employee_id, l.location_id;

3. 非等值连接

# 非等值连接SELECT e.last_name, e.salary, j.grade_levelFROM employees AS e, job_grades AS jWHERE e.salary BETWEEN j.lowest_sal AND j.highest_salORDER BY grade_level;

4. 自连接

# CONCAT() 可实现字段和字符串拼接SELECT CONCAT (worker.last_name,' works for ', manager.last_name)FROM employees worker, employees manager WHERE worker.manager_id = manager.manager_id;

在这里插入图片描述

5. 内连接 INNER JOIN

# 内连接(INNER JOIN)的实现SELECT 字段列表FROM A表 JOIN B表ON 关联条件WHERE 等其他子句;
# 例一SELECT e.employee_id, e.last_name, e.department_id,d.department_id, d.location_idFROM employees e JOIN departments dON e.department_id = d.department_id;# 效果相同:SELECT e.employee_id, e.last_name, e.department_id,d.department_id, d.location_idFROM employees e, departments d WHERE e.department_id = d.department_id;# 例二SELECT employee_id, city, department_nameFROM employees e JOIN departments d ON d.department_id = e.department_idJOIN locations l ON d.location_id = l.location_id;# 效果相同SELECT employee_id, city, department_nameFROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id; 

6. 外连接 LEFT JOIN

#左外连接SELECT 字段列表FROM A表 LEFT JOIN B表ON 关联条件WHERE 等其他子句;#右外连接SELECT 字段列表FROM A表 RIGHT JOIN B表ON 关联条件WHERE 等其他子句;
#左外连接SELECT e.last_name, e.department_id, d.department_id, d.department_nameFROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;

结果包括employees表的全部行,employees表中的Grant没有department_id,departments表中无对应数值,仍显示在结果中(NULL)。
在这里插入图片描述

# 右外连接SELECT e.last_name, e.department_id, d.department_id, d.department_nameFROM employees e RIGHT JOIN departments d ON e.department_id = d.department_id;

结果包括departments表的全部行,employees表中无对应department_id,仍显示在结果中(NULL)。
在这里插入图片描述

7. 合并查询 UNION

SELECT column,... FROM table1UNIONSELECT column,... FROM table2SELECT column,... FROM table1UNION ALLSELECT column,... FROM table2
SELECT * FROM employeesWHERE email LIKE '%a%' OR department_id > 90;SELECT * FROM employeesWHERE email LIKE '%a%'UNION SELECT * FROM employeesWHERE department_id > 90;
SELECT id,cname FROM t_chinamale WHERE csex='男'UNION ALLSELECT id,tname FROM t_usmale WHERE tGender='male';

8. 注意

9. 七种JOIN的实现


在这里插入图片描述

# A ∩ BSELECT e.last_name, e.department_id, d.department_nameFROM employees eJOIN departments d ON e.department_id = d.department_id;


在这里插入图片描述

SELECT e.last_name, e.departments_id, d.department_nameFROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;


在这里插入图片描述

SELECT e.last_name, e.departments_id, d.department_nameFROM employees e RIGHT JOIN departments d ON e.department_id = d.department_id;


在这里插入图片描述

# A - A∩BSELECT e.last_name, e.department_id, d.department_nameFROM employees e LEFT JOIN departments d ON e.department_id = d.department_idWHERE d.department_id IS NULL;


在这里插入图片描述

# B-A∩BSELECT e.last_name, e.department_id, d.department_nameFROM employees e RIGHT JOIN departments d ON e.department_id = d.department_idWHERE e.department_id IS NULL;


在这里插入图片描述

# A∪B SELECT e.last_name, e.department_id, d.department_nameFROM employees eLEFT JOIN departments d ON e.department_id = d.department_idUNION SELECT e.last_name, e.department_id, d.department_nameFROM employees eRIGHT JOIN departments d ON e.department_id = d.department_idWHERE e.department_id IS NULL;


在这里插入图片描述

SELECT e.last_name, e.department_id, d.department_nameFROM employees eLEFT JOIN departments d ON e.department_id = d.department_idWHERE d.department_id IS NULLUNIONSELECT e.last_name, e.department_id, d.department_nameFROM employees eRIGHT JOIN departments d ON e.department_id = d.department_idWHERE e.department_id IS NULL

10.练习

# 1.显示所有员工的姓名,部门号和部门名称。-- 1.1等值连接WHERE,只显示满足连接条件的结果SELECT e.last_name, e.department_id, d.department_nameFROM employees e, departments dWHERE e.department_id = d.department_id;-- 1.2内连接join on,只显示满足连接条件的结果,1/2方法等效SELECT last_name, e.department_id, d.department_nameFROM employees e JOIN departments d ON e.department_id = d.department_id;-- 1.3外连接left join,显示主表左表的全部结果-- 因为存在无主管的员工(deptid和deptname填null),结果比1/2更充分SELECT last_name, e.department_id, d.department_nameFROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;# 2.查询90号部门员工的job_id和90号部门的location_idSELECT e.department_id, e.job_id, d.location_idFROM employees e, departments dWHERE e.department_id = 90 AND e.department_id = d.department_id;-- 先通过join on 连接两表,再添加筛选条件SELECT e.department_id, e.job_id, d.location_idFROM employees e JOIN departments d ON e.department_id = d.department_idWHERE e.department_id = 90;# 3.选择所有有奖金的员工的 last_name , department_name , location_id , citySELECT last_name, d.department_name, d.location_id, city FROM employees e, departments d, locations l WHERE e.commission_pct IS NOT NULL AND e.department_id = d.department_idAND d.location_id = l.location_id; SELECT last_name, d.department_name, d.location_id, city FROM employees e JOIN departments d ON e.department_id = d.department_idJOIN locations l ON d.location_id = l.location_idWHERE commission_pct IS NOT NULL;# 4.选择city在Toronto工作的员工的 last_name , job_id , department_id , department_nameSELECT e.last_name, e.job_id, e.department_id, d.department_nameFROM employees e, departments d, locations l WHERE e.department_id = d.department_idAND d.location_id = l.location_idAND l.city = 'Toronto';SELECT e.last_name, e.job_id, e.department_id, d.department_nameFROM employees e JOIN departments d ON e.department_id = d.department_idJOIN locations l ON d.location_id = l.location_idWHERE l.city = 'Toronto';#5.查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为’Executive’SELECT d.department_name, l.street_address, e.last_name, e.job_id, e.salaryFROM employees e, departments d, locations l WHERE e.department_id = d.department_idAND d.location_id = l.location_idAND d.department_name = 'Executive';SELECT d.department_name, l.street_address, e.last_name, e.job_id, e.salaryFROM employees e JOIN departments d ON e.department_id = d.department_idJOIN locations l ON d.location_id = l.location_idWHERE d.department_name = 'Executive';#6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式employees Emp# manager Mgr#kochhar 101 king 100SELECT e1.last_name, e1.employee_id, e1.manager_id, e2.last_name manager_name FROM employees e1, employees e2 WHERE e1.manager_id = e2.employee_id;SELECT e1.last_name, e1.employee_id, e1.manager_id, e2.last_name manager_name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.employee_id;-- left join on 会包含employee没有manager的结果SELECT emp.last_name employees, emp.employee_id "Emp#", mgr.last_name manager,mgr.employee_id "Mgr#"FROM employees empLEFT OUTER JOIN employees mgrON emp.manager_id = mgr.employee_id;# 7.查询哪些部门没有员工SELECT d.department_id, d.department_nameFROM departments d LEFT JOIN employees e ON d.department_id = e.department_idWHERE e.department_id IS NULL;SELECT d.department_id, d.department_nameFROM departments d WHERE NOT EXISTS (SELECT * FROM employees eWHERE e.department_id = d.department_id);# 8. 查询哪个城市没有部门SELECT l.location_id, l.city FROM locations lLEFT JOIN departments d ON l.location_id = d.location_idWHERE d.location_id IS NULL;# 9. 查询部门名为 Sales 或 IT 的员工信息SELECT e.last_name, e.employee_id, e.department_id, d.department_nameFROM employees e, departments d WHERE e.department_id = d.department_idAND d.department_name IN ('Sales', 'IT');

来源地址:https://blog.csdn.net/RiceVan/article/details/128907870

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯