当前位置:网站首页>MySQL: join query | inner join, outer join
MySQL: join query | inner join, outer join
2022-08-05 07:11:00 【_Sauron】
连接查询
Join query is divided into inner join query and outer join query
场景一:内连接查询
学生、课程、成绩
表的创建
插入数据
查询示例
要查看 zhang sanclassmates' grades:预置条件 uid = 1, cid = 2
But this is not obvious,I don't know which classmate got the grades of which class.Therefore, multi-table join query is required,The normal method requires two statements:
内连接查询:
select a.uid, a.name, a.age, a.sex, c.score from student a
inner join exam c on a.uid=c.uid
Plus filters:
select a.uid, a.name, a.age, a.sex, c.score from student a
inner join exam c on a.uid=c.uid where c.uid = 1 and c.cid = 2;
关于 on a.uid=c.uid How to distinguish between large and small tables?
are classified according to the amount of data,Small tables are the ones with less data,Because the small table needs the whole table search,Then go to the big table search.
The above statement is 先从studentTake everything out of the small tableuid,然后拿着这些uid去examSearch inside the big table.
没有过滤条件:
If you want to see the specific information of the course,Then you need to do another inner join query on the curriculum:
其他示例:To query a course over90The specific information of the students:Just need to change the filter conditions behind
效率问题
To view grade information for all students:使用内连接,然后查看SQL的执行计划,Found to scan firststudent表,Because the table data is small,So as a small table
If it points to query the student grade information of a certain course:
再看SQL执行计划,Found to scan firstexam表去,把examtable as small table
如果把 b.cid=3in the join condition:found and used abovewhere是一样的,所以:
对于inner join内连接,过滤条件写在where的后面和on连接条件里面,效果是一样的
模板:
SELECT a.属性名1,a.属性名2,…,b,属性名1,b.属性名2… FROM table_name1 a
inner join table_name2 b on a.id = b.id
where a.属性名 满足某些条件;
外连接查询
左连接查询
SELECT a.属性名列表, b.属性名列表 FROM table_name1 a LEFT [OUTER] JOIN table_name2 b on
a.id = b.id;
与内连接的区别:把left这边的表所有的数据显示出来,在右表中不存在相应数据,则显示NULL
select a.* from User a left outer join Orderlist b on a.uid=b.uid where a.orderid is null;
如下图:因为给studentA new student has been added to the table,The newly added students do not have courses and grades,所以显示NULL
右连接查询
SELECT a.属性名列表, b.属性名列表 FROM table_name1 a RIGHT [OUTER] JOIN table_name2 b on
a.id = b.id;
Different from inner join: 把right这边的表所有的数据显示出来,在左表中不存在相应数据,则显示NULL
select a.* from User a right outer join Orderlist b on a.uid=b.uid
where b.orderid is null;
执行计划对比:
示例
To find out which student did not take the test,使用in The subquery statement is :
select * from student where uid not in(select distinct uid from exam);
使用左连接查询:
select a.* from student a left join exam b on a.uid=b.uid where b.cid is null;
待补充..
边栏推荐
猜你喜欢
随机推荐
GAN generates anime avatar Pytorch
FPGA parsing B code----serial 4
【Dynamic type detection Objective-C】
How to avoid online memory leaks
Shiny04---DT和进度条在shiny中的应用
TCP的粘包拆包问题+解决方案
(四)旋转物体检测数据roLabelImg转DOTA格式
17-VMware Horizon 2203 virtual desktop-Win10 manual desktop pool floating (seventeen)
合工大苍穹战队视觉组培训Day9——相机标定
DevExpress中针对指定列进行百分比转换
1、Citrix XenDesktop 2203之AD域系统安装(一)
typescript68-索引查询类型(查询多个)
今天虚竹哥又发现了一款好用的国产化API工具
(2022杭电多校六)1012-Loop(单调栈+思维)
怎么样避免线上内存泄漏
Kioxia and Aerospike Collaborate to Improve Database Application Performance
Bluetooth gap protocol
UDP group (multi)cast
【instancetype类型 Objective-C】
Flink Learning 12: DataStreaming API