当前位置:网站首页>数据库多表链接查询的方式
数据库多表链接查询的方式
2022-08-09 14:51:00 【Ran959】
大家好,在日常学习中分享一下所学的笔记,希望可以帮助到大家~
本篇将多表联查分为三部分来介绍:
1. 通过Select子句进行查询
2. 通过内连接 inner join进行查询
3. 通过外连接left join,left outer join,right join,right outer join,union进行查询
一、通过SELECT子句进行多表查询
语法:
select 字段名
from表1,表2 …
where表1.字段 = 表2.字段
and 其它查询条件
例:以学生表student和班级表class为例
Select student.sid, student.sname, student.classid, class.classid, class.classname
from student,class
where student.classid = class.classid
注意:上面的代码中,以两张表相同的字段信息作为条件,进行两个表联查,但在实际开发中不建议这样使用,最好用主外键约束来实现。
二、通过内连接 inner join进行查询
语法:
select 字段名
from表1
inner join 表2
on 表1.字段 = 表2.字段
例:以学生表student和班级表class为例

select student.sid, student.sname, student.classid, class.classid, class.classname
from student
inner join class
on student.classid = class.classid

这种场景下得到的是满足某一条件的student,class内部的数据;正因为得到的是内部共有数据,所以连接方式称为内连接。
三、通过外连接left join,left outer join,right join,right outer join,union进行查询
1.left join
语法:
select 字段名
from表1
left join 表2
on 表1.字段 = 表2.字段
例:以学生表student和班级表class为例

select student.* , class.*
from student
left join class
on student.classid = class.classid
结果如下,class表中不存在的记录填充Null:

这种场景下得到的是student的所有数据,和满足某一条件的class的数据;
2.left outer join(相当于left join + [where 表2.字段 is null])
语法:
select字段名
from表1
left join 表2
on 表1.字段 = 表2.字段
where 表2.字段is null
例:以学生表student和班级表class为例

select student.sid,student.sname,class.classid,class.classname
from student
left join class
on student.classid = class.classid
where class.classid is null
![]()
这种场景下得到的是student中的所有数据减去"与class满足同一条件 的数据",然后得到的student剩余数据
3.right join
语法:
select 字段名
from 表1
right join表2
on 表1.字段 = 表2.字段
例:以学生表student和班级表class为例

select student.* , class.*
from student
right join class
on student.classid = class.classid

这种场景下得到的是class的所有数据,和满足某一条件的student的数据;
4.right outer join(相当于right join + [where 表1.字段 is null])
语法:
select字段名
from 表1
right join 表2
on 表1.字段 = 表2.字段
where 表1.字段is null
例:以学生表student和班级表class为例

select student.sid,student.sname,class.classid,class.classname
from student
right join class
on student.classid = class.classid
where student.classid is null
![]()
这种场景下得到的是class中的所有数据减去 "与student满足同一条件 的数据“,然后得到的class剩余数据;
4.left join union right join
语法:
select 字段名
from表1
left join 表2
on 表1.字段 = 表2.字段
union
select 字段名
from表1
right join表2
on 表1.字段 = 表2.字段
例:以学生表student和班级表class为例

select student.* , class.*
from student
left join class
on student.classid = class.classid
union
select student.* , class.*
from student
right join class
on student.classid = class.classid

这种场景下得到的是满足某一条件的公共记录,和独有的记录
以上分享希望能帮到你,有错误及时指正哦~
边栏推荐
- JS 选项卡切换tab
- [Microservice] Detailed explanation of nacos registration center and configuration center
- 道路标识信息 (RSI)
- After reading the "Redis In-depth Notes" compiled by Tencent bosses in 90 days, I worshipped on the spot.
- 防汛添利器,数字技术筑起抗洪“大堤”
- 启动报错:Caused by: org.apache.ibatis.binding.BindingException汇总解决
- Shell functions and arrays
- go访问私有变量
- Swift中的Error处理
- C语言程序设计笔记(浙大翁恺版) 第七章:函数
猜你喜欢
随机推荐
ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions unless TOP, OFFSET, or FOR XML is also specified
C语言程序设计笔记(浙大翁恺版) 第五周:循环控制
Startup error: Caused by: org.apache.ibatis.binding.BindingException summary solution
[Serilog] Simple .NET logging with fully structured events
Shell编程之循环语句
【Qt5 + OpenGL】glPointSize(10); error: undefined reference to `__imp_glPointSize‘
Two-dimensional array to realize the eight queens problem
极限挑战,如何做到分钟级搭建环境?
注解与反射
[DevOps] jekins configuration (2)
MySQL 原理与优化:Limit 查询优化
xshell7连接工具下载
shell------常用小工具,sort,uniq,tr,cut
Swift中的Error处理
太厉害了!华为大牛终于把MySQL讲的明明白白(基础+优化+架构)
C语言程序设计笔记(浙大翁恺版) 第四周:循环
docker安装seata(指定配置文件、数据库、容器数据卷等)
爬虫处理乱码问题
【基础版】整数加减乘除计算器
【NodeJs篇】关于path 路径模块的学习和使用









