当前位置:网站首页>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;

待补充..
边栏推荐
- Mysql为什么 建立数据库失败
- 【JVM调优】Xms和Xmx为什么要保持一致
- 17-VMware Horizon 2203 virtual desktop-Win10 manual desktop pool floating (seventeen)
- Shiny02---Shiny exception solution
- Libpq 是否支持读写分离配置
- Vulnhub靶机:HA_ NARAK
- 自媒体人一般会从哪里找素材呢?
- 400 times performance improvement 丨 swap valuation optimization case calculation
- IO process thread -> communication between processes -> day7
- 给网站套上Cloudflare(以腾讯云为例)
猜你喜欢
随机推荐
PCI Pharma Services宣布斥资数百万美元扩建英国制造设施,以满足市场对支持肿瘤治疗的全球高效药制造服务日益增长的需求
RNote108---Display the running progress of the R program
(2022杭电多校六)1010-Planar graph(最小生成树)
蓝牙gap协议
开源中国活动合作说明书
Flink学习10:使用idea编写WordCount,并打包运行
专用机终端安装软件后报IP冲突
Flink学习12:DataStreaming API
开启防火墙iptable规则后,系统网络变慢
香港国际珠宝展及香港国际钻石、宝石及珍珠展揭幕
合工大苍穹战队视觉组培训Day9——相机标定
Promise (3) async/await
工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯
Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
Shared memory + inotify mechanism to achieve multi-process low-latency data sharing
typescript59-泛型工具类型(partial )
对数据类型而言运算符无效。运算符为 add,类型为 text。
TCP的粘包拆包问题+解决方案
400 times performance improvement 丨 swap valuation optimization case calculation
(JLK105D)中山爆款LED恒流电源芯片方案









