Mysql 数据库 常用语句 三:多表间接

目录

准备:

一,多表连接

1,左连接

2,右连接 ​

3,内连接 ​

4,外连接 ​(全连接)

5,左连接减内连接 ​(左独有)

6,右连接减内连接 ​(右独有)

7,外连接减内连接 ​


准备:

stu 类

teacher 类

一,多表连接

1,左连接

select * from tableA ​ left join tableB ​

on a.key=b.key

注:(1)左连接是以left join 为基准进行查询,left join 左边的表为左表,右边的表为右表。左连接查询的就是左表的全部信息和符合查询条件的信息(例如下图的 s.tid=t.tid,即左右两表符合关联查询条件的信息)两部分。
(2)如果左表展示出来的全部信息,右表没有相对应的信息,则右表记录为 null 。

 

2,右连接 ​

select * from tableA a ​ right join tableB b ​

on a.key=b.key;

注:(1)右连接是以right join 为基准进行查询,right join 左边的表为左表,右边的表为右表。右连接查询的就是右表的全部信息和符合查询条件的信息(例如下图的 s.tid=t.tid,即左右两表符合关联查询条件的信息)两部分。
(2)如果右表展示出来的全部信息,左表没有相对应的信息,则左表记录为 null 。

 

3,内连接 ​

select * from tableA a ​ inner join tableB b ​

on a.key=b.key;

注:inner join 内连接,展示的是两表之间的交集 

 

4,外连接 ​(全连接)

select * from tableA a left join tableB b

on a.key=b.key ​

union //去重 ​

select * from tableA a right join tableB b

on a.key=b.key;

 

5,左连接减内连接 ​(左独有)

select * from tableA a ​ left join tableB b ​

on a.key=b.key ​

where b.key is null;

 

6,右连接减内连接 ​(右独有)

select * from tableA a ​ right join tableB b ​

on a.key=b.key ​

where a.key is null;

 

7,外连接减内连接 ​

select * from tableA a ​ left join tableB b

on a.key=b.key ​

where b.key is null ​

union ​

select * from tableA a ​ right join tableB b

on a.key=b.key ​

where a.key is null;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值