SQL 多表查询

多表查询的分类

  • 等值连接 VS 非等值连接
  • 自连接 VS 非自连接
  • 内连接 VS 外连接

等值连接

关联的表有连接字段

select ci.vbillno, ci.infodate, ci.bankaccount, ci.oppbankaccount,bb.accname as oppunitname, oo.code as pk_org, ci.moneyy, ci.memo, bc.code as currtypeCode
from cmp_informer ci
left join bd_currtype bc on bc.pk_currtype = ci.pk_currtype
left join org_orgs oo on oo.pk_org = ci.pk_org
left join bd_bankaccbas bb on bb.accnum = ci.oppbankaccount
where ci.dr = 0
	and ci.direction = 'receivemoney'
	and ci.generateflag ='hasrelease'

非等值连接

关联的表没有连接字段

select e.last_name,e.salary,j.grade_level
from employees e,job_grades j
where e.salary between j.lowest_sal and j.highest_sal

在这里插入图片描述

自连接

自己连接自己

select emp.employee_id,emp.last_name,mgr.empliyee_id,mgr.last_name
from employees emp,employees mgr
where emp.manager_id = mgr.employee_id

在这里插入图片描述

非自连接

不同的表连接查询

内连接

只查询左表和右表满足where条件的数据,也就是合并具有同一列的两个以上的表的行,结果集中不包含一个表与另一个表不匹配的行

select last_name,dapartment_name,city
from employees e 
inner join departments d on e.department_id = d.department_id
inner join locations l on d.locateion_id = l.location_id

外连接

只查询左表和右表不满足where条件的数据,也就是合并具有同一列的两个以上的表的行,结果集中不包含一个表与另一个表不匹配的行之外,还查询到左表或右表中不匹配的行

左外连接

两个表在连接过程中除了返回满足连接条件的行以外,还返回左表中不满足条件的行

select last_name,dapartment_name,city
from employees e 
left outer join departments d on e.department_id = d.department_id
left outer join locations l on d.locateion_id = l.location_id

右外连接

两个表在连接过程中除了返回满足连接条件的行以外还返回右表中不满足条件的行

select last_name,dapartment_name,city
from employees e 
right outer join departments d on e.department_id = d.department_id
right outer join locations l on d.locateion_id = l.location_id

满外连接

mysql 不支持 full outer join

-- oracle
select last_name,dapartment_name
from employees e 
full outer join departments d on e.department_id = d.department_id

-- mysql
-- UNION 会执行去重操作  UNION ALL 不会执行去重操作
--左上图 UNION ALL 右中图
select last_name,dapartment_name
from employees e 
left join departments d on e.department_id = d.department_id
union all
select last_name,dapartment_name
from employees e 
right join departments d on e.department_id = d.department_id
where e.department_id IS NULL;

--左中图 UNION ALL 右上图
select last_name,dapartment_name
from employees e 
left join departments d on e.department_id = d.department_id
where d.department_id IS NULL;
union all
select last_name,dapartment_name
from employees e 
right join departments d on e.department_id = d.department_id

--右下图 = 左中图 UNION ALL 右中图
select last_name,dapartment_name
from employees e 
left join departments d on e.department_id = d.department_id
where d.department_id IS NULL;
union all
select last_name,dapartment_name
from employees e 
right join departments d on e.department_id = d.department_id
where e.department_id IS NULL;

SQL连接 JOIN

在这里插入图片描述

SQL99 语法新特性 自然连接 NATURAL JOIN & USING

-- 自然连接 NATURAL JOIN 
-- 可以把自然连接理解为SQL92中的等值连接,它会帮你自动查询两张连接表中所有相同的字段,然后进行等值连接。
--不适用于两张表中有多个关联字段,你只想要某一个字段关联
select employee_id,last_name,department_name
from employees e
NATURAL JOIN departments d
--等同于
select employee_id,last_name,department_name
from employees e 
join departments d on e.department_id = d.department_id
and e.manager_id = d.manager_id

-- USING连接
--USING(同名字段),简化JOIN ON
--不适用于自连接,也就是自己引用自己的表
select employee_id,last_name,department_name
from employees e 
join departments d USING(department_id)
--等同于
select employee_id,last_name,department_name
from employees e 
join departments d on e.department_id = d.department_id

相关推荐

  1. Sql查询

    2024-03-15 22:34:01       37 阅读
  2. SQL查询

    2024-03-15 22:34:01       30 阅读
  3. SQL查询

    2024-03-15 22:34:01       25 阅读
  4. sql server查询

    2024-03-15 22:34:01       65 阅读
  5. SQL进阶:查询

    2024-03-15 22:34:01       66 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 22:34:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 22:34:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 22:34:01       87 阅读
  4. Python语言-面向对象

    2024-03-15 22:34:01       96 阅读

热门阅读

  1. Lucene 分词 示例代码

    2024-03-15 22:34:01       36 阅读
  2. 字符串的排列(LeetCode 567)

    2024-03-15 22:34:01       38 阅读
  3. jupyterlab 设置

    2024-03-15 22:34:01       36 阅读
  4. 嵌入式面经-TCP/UDP

    2024-03-15 22:34:01       36 阅读
  5. 开发小程序不破产!预算周期大揭秘,小白必看

    2024-03-15 22:34:01       37 阅读
  6. 【数据结构学习笔记】选择排序

    2024-03-15 22:34:01       47 阅读
  7. 回调函数的介绍

    2024-03-15 22:34:01       38 阅读
  8. 【力扣二刷思路】DAY3

    2024-03-15 22:34:01       34 阅读
  9. 使用回溯法解决leetcode 1219

    2024-03-15 22:34:01       38 阅读