4、多表查询

Mysql 多表查询

Select * from tablea inner join tableb on tablea.id = tableb.id where tableb.id is null;

Select * from tablea eft join tableb on tablea.id = tableb.id where tableb.id is null;

1、多表查询介绍

在实际应用中
MySQL大部分情况下,查询语句都会涉及到多张表格:
其中多表查询的分类有:内连接,外连接和交叉连接

A内连接:
join, inner join

B外链接:
left join, left outer join, right join, right outer join, union

C交叉连接:
cross join


2、实例

假设有两张表:

TableA:


image.png

TableB:


image.png
2.1 内连接(只有一种场景)

Inner join 或者 join(等同于 inner join)

Select a., b. from tablea a
inner join tableb b
On a.id = b.id;
或者:

Select a.,b. from tablea a
Join tableb b
On a.id = b.id;

image.png

应用场景:

image.png

注意:
有一种连接为
自然连接:
nature join, 假如执行:

Select * form Tablea a nature join tableb b;

这种连接和内连接相似,但是输出的结果中,会将相同的列去掉,即上述中
id列只会有一列,不会有相同的两列。

2.2 外连接(六种场景)
2.2.1 left join 或者left outer join(等同于left join)

select a., b. from tablea a

left join tableb b

on a.id = b.id

或者

select a., b. from tablea a

left outer join tableb b

on a.id = b.id

结果如下,
TableB中更不存在的记录填充Null:

image.png

应用场景
:


image.png

这种场景下得到的是
A的所有数据,和满足某一条件的B的数据;

2.2.2 [left join 或者left outer join(等同于left join)] + [where B.column is null]

select a.id aid,a.age,b.id bid,b.name from tablea a
left join tableb b
on a.id = b.id
Where b.id is null

结果如下
:

image.png

应用场景
:

image.png

这种场景下得到的是
A中的所有数据减去"与B满足同一条件 的数据",然后得到的A剩余数据;

2.2.3 right join 或者fight outer join(等同于right join)

select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id

结果如下,
TableA中更不存在的记录填充Null:


image.png

应用场景
:

image.png

这种场景下得到的是
B的所有数据,和满足某一条件的A的数据;

2.2.4 [left join 或者left outer join(等同于left join)] + [where A.column is null]

select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id
where a.id is null

结果如下
:

[


image.png

应用场景
:

image.png

这种场景下得到的是
B中的所有数据减去 "与A满足同一条件 的数据“,然后得到的B剩余数据;

2.2.5 full join (mysql不支持,但是可以用 left join union right join代替)

select a.id aid,a.age,b.id bid,b.name from tablea a
left join tableb b
on a.id = b.id
union
select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id

union过后,重复的记录会合并(id为2,3,4的三条记录),所以结果如下:


image.png

应用场景:

image.png

这种场景下得到的是满足某一条件的公共记录,和独有的记录

2.2.6 full join + is null(mysql不支持,但是可以用 (left join + is null) union (right join+isnull代替)****

select a.id aid,a.age,b.id bid,b.name from tablea a
left join tableb b
on a.id = b.id
where b.id is null
union
select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id
where a.id is null

再添加一个
where语句进行扩充

结果如下
:

image.png

应用场景
:

image.png

这种场景下得到的是
A,B中不满足某一条件的记录之和


:上面共有其中七(2^3-1)种应用场景,还有一种是全空白,那就是什么都不查,七种情形包含了实际应用所有可能的场景

2.3 交叉连接 (cross join)

2.3.1 实际应用中还有这样一种情形,想得到A,B记录的排列组合,即笛卡儿积,这个就不好用集合和元素来表示了。需要用到cross join:

select a.id aid,a.age,b.id bid,b.name from tablea a
cross join tableb b

image.png

**2.3.2 还可以为cross join指定条件 (where):****
**
select a.id aid,a.age,b.id bid,b.name from tablea a
cross join tableb b
where a.id = b.id

结果如下

image.png


:这种情况下实际上实现了内连接的效果

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,839评论 18 399
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,264评论 0 7
  • 一. Java基础部分.................................................
    wy_sure阅读 3,874评论 0 11
  • 我不太愿意牵强的生活 就像生活对待我的态度一样 过分苛刻 我只能牵着昨天的手和今天的现实走 一路的风景从来没看过 ...
    小售阅读 311评论 0 1
  • 家,有着深远的意义,是心灵的避风港,是让自己内心最舒坦的一个地方。 小时候,家离学校很近。来回也就几分钟的路程,沿...
    漾舟阅读 138评论 0 0