当前位置:网站首页>Database multi-table link query method
Database multi-table link query method
2022-08-09 16:12:00 【Ran959】
大家好,Share the notes you learn as part of your daily study,希望可以帮助到大家~
This article will introduce the multi-table joint query into three parts:
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
注意:上面的代码中,The condition is based on the same field information in the two tables,Perform a join query between two tables,But this is not recommended in actual development,最好用主外键约束来实现.
二、通过内连接 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
What you get in this scenario is满足某一条件的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
结果如下,classRecords that do not exist in the table are populatedNull:
What you get in this scenario isstudent的所有数据,and satisfy a certain conditionclass的数据;
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
What you get in this scenario isstudentAll data in are subtracted"与classmeet the same conditions 的数据",然后得到的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
What you get in this scenario isclass的所有数据,and satisfy a certain conditionstudent的数据;
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
What you get in this scenario isclassAll data in are subtracted "与studentmeet the same conditions 的数据“,然后得到的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
What is obtained in this scenario is a public record that satisfies a certain condition,and unique records
Hope the above sharing can help you,Correct any mistakes in time~
边栏推荐
- 极限挑战,如何做到分钟级搭建环境?
- Mysql两个引擎对比
- 【胡扯】量子力学与单线程
- 【LeetCode】1898. 可移除字符的最大数目
- 浅谈ArraryList的浅克隆和深克隆
- Redis6.2.1配置文件详解
- Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
- 在量化交易过程中,散户可以这样做
- DBCO-PEG-DSPE, Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne, Reaction Without Copper Ion Catalysis
- C语言程序设计笔记(浙大翁恺版) 第二周:计算
猜你喜欢
随机推荐
Servlet的生命周期
C语言程序设计笔记(浙大翁恺版) 第四周:循环
代码随想录笔记_动态规划_377组合总和IV
vivo手机上的系统级消息推送平台的架构设计实践
数据库多表链接查询的方式
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基MW:1000
DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-PEG-Aldehyde MW: 1000
【STM32】TCL2543CN 12位11通道ADC芯片stm驱动程序,使用32自带SPI实现
卷积神经网络表征可视化研究综述(1)
OpenCV - matchTemplate image template matching
量化投资者是如何获取实时行情数据的呢?
爬虫处理乱码问题
OpenSSF的开源软件风险评估工具:Scorecards
08-Lock版的生产者消费者问题
docker安装seata(指定配置文件、数据库、容器数据卷等)
网站授权QQ登录
怎么才可以知道量化程序化交易是否有效?
Grad CAM model visualization
运算符学习
二维数组实现八皇后问题