当前位置:网站首页>MySql中on与where的区别个人总结——分清楚条件应该写在哪里
MySql中on与where的区别个人总结——分清楚条件应该写在哪里
2022-07-21 16:46:00 【追逐梦想永不停】
一、前言
最近工作中,遇到了这样一个场景:
有2个数据库表,
其中一个是班级课程表class_course,记录了一个班级中有多少门课程,字段如下:
如图,a班有4门课,b班有2门课;
另一个是学生上课记录表student_course,记录了一个学生已经上了哪几门课,字段如下:
如图,zhangsan01上了a班的3门课,b班的1门课;
lisi上了b班的2门课;
问题:如何查询出,zhangsan01是否上完了a班所有的课程?
二、解决思路
首先,本人想到,可以根据course_id,用class_course表左联student_course,然后筛选出a班的、zhangsan01的记录;
其中如果有联查后student_course.course_id为null的,就说明zhangsan01没有上过这门课,就说明zhangsan01没有上完a班所有的课程;
如果不存在null,就说明zhangsan01上完了a班所有的课程。
如图:
只要能用sql查询出这样的List<String>
,然后java里循环下,发现有为null的,说明zhangsan01确实没上a班的a4课,也就能得出zhangsan01没有上完a班的所有课程的结论。
这样就可以实现需求了,下一个问题是sql怎么写了。
三、踩坑流程——错误sql
1.条件全写在on里面
select b.course_id,a.* from class_course a left join student_course b
on a.course_id=b.course_id and a.class_id='a' and b.student_id='zhangsan01';
本人最开始想到的sql就是这样,但是查询到的结果不符合需求:
虽然on里限定了条件a.class_id=‘a’,但是不知道为什么结果还是有b班的数据,这样查询的还是不对。
2.主要条件写where里面
select b.course_id,a.* from class_course a left join student_course b
on a.course_id=b.course_id
where a.class_id='a' and b.student_id='zhangsan01';
首先左联时要on a.course_id=b.course_id ,这个是毫无疑问的;
然后由于条件写on里不对,因此本人尝试写在where里,但是结果还是不对:
因为样例数据中,zhangsan01没有上a4课,所以应该有一行null的;
但是这样并没有查到null,也不对。
四、正确sql
一部分条件在on里,一部分条件在where里:
select b.course_id,a.* from class_course a left join student_course b
on a.course_id=b.course_id and a.class_id='a' and b.student_id='zhangsan01'
where a.class_id='a';
结果如图:
sql中,on里限制了只用a班的、zhangsan01的数据进行联查;
不过如果只在on里限制的话,由于是左联,因此class_course表里的其它数据也会显示出来,只不过连接的student_course表里的数据为空而已,不符合要求;
所以要在where中限制,只展示a班的,这样得到的结果就是想要的了;
需要注意,where中不能再限制只展示zhangsan01的了,如果加上后,最后一行null就展示不出来了。
上面的sql中,增加a.*是为了容易理解些,实际上最终要的sql是:
select b.course_id from class_course a left join student_course b
on a.course_id=b.course_id and a.class_id='a' and b.student_id='zhangsan01'
where a.class_id='a';
执行这句sql后,java中可以得到长度为4的List<String>
,遍历后,发现有为null的,就能得出zhangsan01没有上完a班的所有的课程的结论,就实现了需求。
五、总结
1.on中的条件,是决定根据什么进行联查(on a.course_id=b.course_id
) 、用哪些数据进行联查( and a.class_id='a' and b.student_id='zhangsan01'
)用的。
2.where中的条件,是决定最后数据怎么过滤用的(where a.class_id='a'
)。
3.联查时需要注意条件写在on里还是where里;如本文中的左联查询,只写在on里不能满足需求,左表中其它的数据也会连接空数据显示出来;所以on里与where里都需要写条件。
边栏推荐
- R language uses the mean function to calculate the relative frequency of the specified variables in the sample (observation) data: calculate the proportion of the observation samples in the dataframe
- Chiitoitsu
- C语言实型数据的存在形式以及合法性(浮点数)
- tsconfig. JSON cannot find any input in the configuration file. What should I do?
- 性能测试----测试执行
- 着手社区建设掌握的两个概念
- DS排序--快速排序
- Explain pytorch visualization tool visdom in detail (I)
- Three processes of PMP preparation in 2022 -- Challenge (1)
- Matlab drawing summary of mathematical modeling
猜你喜欢
迪拜推出国家元宇宙战略
Leetcode 1288. 删除被覆盖区间(可以,已解决)
如何让游戏中的随机因素重新赢得玩家信任
DS二叉树—二叉树结点的最大距离
tsconfig. JSON cannot find any input in the configuration file. What should I do?
ROS manipulator movelt learning notes 1 | basic preparation
强连通分量
Json文件编辑器
Read the paper with me - multi model text recognition network
Formal Languages and Compilers 笔记&教程 第二章 上下文无关语言 (Context-Free Languages)
随机推荐
移动端测试用例的编写
DS二叉树—二叉树结点的最大距离
Jmter -- database performance test
Mutual certification of product compatibility between tapdata and Youxuan database
wallys/new product/DR7915/MT7915+MT7975/WiFi6 MiniPCIe Module 2T2R
Yiwen teaches you five tips for detecting whether MOS tubes are good or bad "suggestions collection"
智能运维场景解析:如何通过异常检测发现业务系统状态异常
"Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
Web3 n'a jamais échappé à ces vieux géants.
无线充电原理与QI协议详解[通俗易懂]
How to change the console font to console?
如何设计产品MVP实现价值最大化
活动报名:如何零基础快速上手开源的 Tapdata Live Data Platform?
R language ggplot2 visualization: visualize the scatter diagram, add formula labels to the data points in the scatter diagram, and use geom of ggrep package_ text_ The repl function avoids overlapping
Detailed explanation of wireless charging principle and Qi protocol [easy to understand]
Programming in CoDeSys to realize serial communication
特征选择小结:过滤式、包裹式、嵌入式
跟我读论文丨Multi-Model Text Recognition Network
Differences among mdladdress, userbuffer and systembuffer of IRP structure
Kubernetes static storage and dynamic storage