当前位置:网站首页>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~
边栏推荐
猜你喜欢

docker安装单机版redis、集群版redis

DMPE-PEG-Mal Maleimide-PEG-DMPE 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺

navicat for Oraclel链接oracle 报错oracle library is not loaded的解决办法

Docker安装MySQL详细步骤

C语言——指针和数组、指针数组和数组指针、指针和二维数组

MySQL 原理与优化:Limit 查询优化

经典面试题 之 TCP 三次握手/ 四次挥手

【OpenGL】四、OpenGL入门总结:LearnOpenGL CN教程中关于欧拉角公式推导

暴雨天,看天翼云如何“快准稳”防涝

浅析Servlet三大容器的常用方法及其作用域
随机推荐
量化投资者是如何获取实时行情数据的呢?
DBCO-PEG-DSPE, Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne, Reaction Without Copper Ion Catalysis
网站授权QQ登录
运算符学习
【胡扯】量子力学与单线程
生产者/消费者问题(线程信号)
如何通过股票量化交易接口实现盈利稳定?
用户如何正确去认识程序化交易?
什么是模板引擎?常见的模板引擎有哪些?thymeleaf的常用指令介绍。
[Mysql]--事务、事务的隔离级别、脏读、不可重复读、幻读解析
Redis6.2.1配置文件详解
【Qt5 + OpenGL】glPointSize(10); error: undefined reference to `__imp_glPointSize‘
和月薪5W的测试聊过后,才知道自己一直在打杂...
Startup error: Caused by: org.apache.ibatis.binding.BindingException summary solution
基于FPGA的FIR滤波器的实现(3)—采用Filter Design & Analysis设计
物联网技术概论:1~7章汇总(西安交通大学)
经典面试题 之 JVM调优
OpenCV - Matrix Operations Part 3
Hudi Spark-Shell 实战
C语言程序设计笔记(浙大翁恺版) 第五周:循环控制