4、多表查询

2017-11-06  本文已影响0人  24_yu

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


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

上一篇 下一篇

猜你喜欢

热点阅读