当前位置:网站首页>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;
待补充..
边栏推荐
- typescript64-映射类型
- GAN generates anime avatar Pytorch
- typescript68-索引查询类型(查询多个)
- Week 8 Document Clustering(文本聚类)
- Using printf function in STM32
- 腾讯业务安全岗 IDP 谈话总结
- typescript65-映射类型(keyof)
- Advanced Redis
- RNote108---Display the running progress of the R program
- 栈与队列的基本介绍和创建、销毁、出入、计算元素数量、查看元素等功能的c语言实现,以及栈的压入、弹出序列判断,栈结构的链式表示与实现
猜你喜欢
随机推荐
17-VMware Horizon 2203 虚拟桌面-Win10 手动桌面池浮动(十七)
合工大苍穹战队视觉组培训Day9——相机标定
(2022杭电多校六)1012-Loop(单调栈+思维)
binary search tree problem
在STM32中使用printf函数
MySQL: JDBC programming
GAN generates anime avatar Pytorch
腾讯业务安全岗 IDP 谈话总结
女生做软件测试会不会成为一个趋势?
It turns out that Maya Arnold can also render high-quality works!Awesome Tips
基于快速行进平方法的水面无人船路径规划
MySQL:连接查询 | 内连接,外连接
(2022杭电多校六)1010-Planar graph(最小生成树)
[Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)
蓝牙gap协议
693. 行程排序
UDP group (multi)cast
RK3568 environment installation
3555. 二叉树
Mysql为什么 建立数据库失败