美文网首页
4、多表查询

4、多表查询

作者: 24_yu | 来源:发表于2017-11-06 23:04 被阅读0次

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


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

相关文章

  • 4、多表查询

    Mysql 多表查询 Select * from tablea inner join tableb on tab...

  • SQLAlchemy(四)

    知识要点: 1.多表查询 2.原生SQL的查询 多表查询 在MySQL中我们讲了多表查询,在SQLAlchemy中...

  • python面试题01

    1、什么是多表关联查询,有几种多表关联的查询方式,分别是什么? 多表关联查询概念: 多表关联查询分类:1.1内连接...

  • MySQL-6:查询语句

    1、DQL:查询语句: 1.排序查询 2.聚合函数 3.分组查询 4.分页查询 2、约束3、多表之间的关系4、范式...

  • 数据库基本操作3.0

    今日内容 多表查询 \\ 事务DCL 多表查询: 事务 DCL:

  • MySql : 三、 多表查询和事务

    前言 本篇主要介绍了数据库中多表查询以及事务相关的知识。 目录 一、多表查询二、子查询三、事务 一、多表查询 1....

  • Oracle详解(Ⅱ):世界上目前已知最好的关系型数据库

    多表查询 多表连接基本查询 使用一张以上的表做查询就是多表查询 这样会出现的结果就是:笛卡儿积连接查询的时候一般在...

  • spring-data-jpa 复杂查询:使用

    单表查询 多表查询

  • 4.MySQL多表&事务

    主要内容 1 . 多表查询2 . 事务3 . DCL 多表查询: 事务 DCL:

  • MySQL约束课堂笔记

    今日内容 一、DQL:查询语句2、排序查询3、聚合函数4、分组查询5、分页查询二、约束三、多表之间的关系 DQL:...

网友评论

      本文标题:4、多表查询

      本文链接:https://www.haomeiwen.com/subject/fpjlmxtx.html