MySQL多表查询的案例详解

作者:T,a,o 时间:2024-01-24 13:11:26 

多表查询

案列说明

笛卡尔积的理解

select id,department_name
from employees,departments;#错的

select id,department_id,department_name
from employees CROSS JOIN departments;#错的

每个员工和每个部门匹配了一遍(查出的条目数=id数*department数)

错误原因:缺少连接条件

笛卡尔积的解决

编写连接条件: 表1.列 = 表二.列(若多个表连接,至少要用n-1个连接条件)

select id,employees.name,department_name
from employees,departments
WHERE employees.name = departments.name;

注:如果要显示的列在要查询的表中名字一样,则要表明,是出自哪个表, eg: employees.name

建议在多表查询时,标明显示的是哪个表的信息 (优化)

优化:可以在FROM后使用表的别名,但是一旦使用别名,后续就一定要都用别名

多表查询的分类

等值连接和非等值连接

  • 等值连接:上述的带有=的

  • 非等值连接:没有=的

select t1.id,t1.name,t2.grade
from employees t1,departments t2
WHERE ti.salary BETWEEN t2.lowest_salary AND t2.highest_salary ;#非等值

自连接和非自连接

  • 非自链接:表1和表2连接

  • 自链接:表1和自己连接

#显示员工(t1)和其管理者(t2)的基本信息
select t1.id,t1.name,t2.id,t2.name
from employees t1,employees t2#一个表看作两个表
WHERE t1.manage_id = t2.id ;#自连接

内连接和外连接

  • 内连接:合并含有同一列的表,结果不包括一个表与另一个表不匹配打的行

  • 外连接:合并含有同一列的表,结果除了内连接的结果还查询不匹配的行

外连接的分类:左外连接(左表多,补右边),右外连接(右表多,补左边),满外连接

SQL92:使用(+)创建连接

内连接:见上

外连接:左表有数据不匹配,在右表加(+);反之,在左表加(+),但是MySQL不支持

WHERE t1.department_id = t2.department_id(+)#左连接

SQL99:使用JOIN...ON的方式

内连接

select t1.id,t1.name,t2.department_name,t3.environment
from employees t1 JOIN departments t2
ON t1.department_id = t2.department_id
JOIN locations t3#加入第二个人表
ON t2.department_location = t3.department_location;

外连接

使用OUTER JOIN...ON...

  • 左外连接:LEFT OUTER JOIN

  • 右外连接:RIGHT OUTER JOIN

  • 满外连接:FULL OUTER JOIN(MySQL不支持)

select t1.name,t2.department_name#左外连接
from employees t1 LEFT OUTER(可省略) JOIN departments t2
ON t1.department_id = t2.department_id;

UMION的使用

合并查询结果

SELECT colum... FROM table1
UNION (ALL)
SELECT colum... FROM table2
  • UNION操作符

两个查询结果的并集,去重(效率低)

  • UNION ALL操作符(推荐)

两个查询结果的并集,不去重(效率高)

7种SQL JOINS的实现

MySQL多表查询的案例详解

中图(内连接):

select t1.name,t2.department_name
from employees t1 JOIN departments t2
ON t1.department_id = t2.department_id;

左上图(左外连接):

select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id;

右上图(右外连接):

select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id;

左中图:

select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t2.department_id IS NULL;

右中图:

select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t1.department_id IS NULL;

左下图(满外连接):

#方式一:左上图 UNION ALL 右中图
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
UNION ALL
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t1.department_id IS NULL;

#方式二:左中图 UNION ALL 右上图
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t2.department_id IS NULL
UNION ALL
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id;

右下图:

#左中图 UNION ALL 右中图
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t2.department_id IS NULL
UNION ALL
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t1.department_id IS NULL;

SQL语法新特性

自然连接

使用关键字:NATURAL JOIN(不灵活),自动查询表中所有相同字段,然后进行等值连接

USING连接(不适用于自连接)

使用关键字:USING(同名字段),将表中相同名字的字段自动等值连接

select t1.name,t2.department_name
from employees t1 JOIN departments t2
ON t1.department_id = t2.department_id;
等价于
select t1.name,t2.department_name
from employees t1 JOIN departments t2
USING(department_id);

来源:https://www.cnblogs.com/wht-de-bk/archive/2022/03/04/15966054.html

标签:MySQL,多表,查询
0
投稿

猜你喜欢

  • PyCharm上安装Package的实现(以pandas为例)

    2021-09-21 12:26:30
  • Python如何设置指定窗口为前台活动窗口

    2022-01-22 02:20:02
  • 详解如何使用Python网络爬虫获取招聘信息

    2021-09-28 06:58:17
  • ASP函数验证带小数点数字格式

    2010-01-02 20:41:00
  • 禁止在网页里面是用搜狗的云输入法

    2009-11-29 15:50:00
  • 12种最常用的网页编程语言简介(值得收藏)

    2023-01-29 17:40:48
  • Python装饰器原理与基本用法分析

    2023-04-29 10:46:32
  • JavaScript封装弹框插件的方法

    2024-04-30 10:20:32
  • 浅析python标准库中的glob

    2023-08-04 02:39:10
  • python如何爬取网页中的文字

    2021-04-05 03:55:53
  • Python实战购物车项目的实现参考

    2021-09-28 11:06:29
  • 在JS中解析HTML字符串示例代码

    2024-04-19 11:02:47
  • Flask框架各种常见装饰器示例

    2023-08-25 01:07:45
  • django foreignkey外键使用的例子 相当于left join

    2021-04-17 15:52:33
  • 一文带你搞懂Python中的数据容器

    2021-05-25 16:09:59
  • JS模仿手机端九宫格登录功能实现代码

    2024-04-17 10:33:21
  • python调用Delphi写的Dll代码示例

    2023-07-25 09:33:53
  • 基于python爬取链家二手房信息代码示例

    2022-09-01 18:36:25
  • 远程部署工具Fabric详解(支持Python3)

    2023-10-26 14:05:18
  • python实现串口通信的示例代码

    2023-08-04 03:44:24
  • asp之家 网络编程 m.aspxhome.com