SQL 多表查询

本文详细介绍了SQL中的多表连接类型,包括等值连接、非等值连接、自连接、内连接、外连接(左外、右外、满外)、以及SQL99语法中的自然连接和USING的使用方法。通过实例展示了如何在实际查询中应用这些连接方式。
摘要由CSDN通过智能技术生成

多表查询的分类

  • 等值连接 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
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值