常见通用的Join查询

sql执行顺序(以下示例,从上到下是有顺序的)

咱们写的sql语句
select distinct
	<select_list>
from
	<left_table> <join_type>
join
	<right_table> on <join_condition>
where
	<where_condition>
group by
	<group_by_list>
having
	<having_condition>
order by
	<order_by_condition>
limit
	<limit number>
机器的读取顺序
from <left_table>
on <join_condition>
<join_type> join <right_table>
where <where_condition>
group by <group_by_list>
having <having_condition>
select
distinct <select_list>
order by <order_by_condition>
limit <limit_number>
图示(顺序从左到右)

SQL解析,顺序从左到右

join图

内连接

select <select_list> from tableA A inner join tableB B on A.key=B.key
内连接

左连接

select <select_list> from tableA A left join tableB B on A.key=B.key
左连接

右连接

select <select_list> from tableA A right join tableB B on A.key=B.key
右连接

左外连接

select <select_list> from tableA A left join tableB B on A.key=B.key where B.key is null
左外连接

右外连接

select <select_list> from tableA A right join tableB B on A.key=B.key where A.key is null
右外连接

全连接

select <select_list> from tableA A full outer join tableB B on A.key=B.key

在mysql上,这句因为full outer运行了会报错,所以mysql上执行时要使用union关键字把第二种和第三种语句结合起来使用(union会自动去掉重复的部分):

select <select_list> from tableA A left join tableB B on A.key=B.key
union
select <select_list> from tableA A right join tableB B on A.key=B.key

全连接

我也不知道叫什么连接

select <select_list> from tableA A full outer join tableB B on A.key=B.key where A.key is null or B.key is null
同上,这句也会报错,同样使用union把第四种和第五种语句结合起来用:

select <select_list> from tableA A left join tableB B on A.key=B.key where B.key is null
union
select <select_list> from tableA A right join tableB B on A.key=B.key where A.key is null

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TandK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值