当前位置:网站首页>Ali three sides: what is the difference between using on or where in the left join associated table and the condition
Ali three sides: what is the difference between using on or where in the left join associated table and the condition
2022-06-28 11:42:00 【Java enthusiast】
Before, a coder went to Alibaba for an interview , Asked about LEFT JOIN Used in association table ON still WHERE What's the difference with the conditions , It was answered soon , But ask what caused this situation , I didn't answer for a while .
Now let's talk about it. , Want to pass A left B join on and The latter condition makes the two records found become one , But there are still two .
It turned out join on and The number of result records will not be filtered , Only according to and Whether or not the condition after is displayed B The record of the table ,A The record of the table is bound to show .
No matter and The back is A.id=1 still B.id=1, All show A All the records in the table , And associated display B In the corresponding A In the table id by 1 Or B In the table id by 1 The record of .
function sql :
select * from student s left join class c on s.classId=c.id order by s.id
function sql :
select * from student s left join class c on s.classId=c.id and s.name=" Zhang San " order by s.id
function sql :
select * from student s left join class c on s.classId=c.id and c.name=" Class 3, grade 3 " order by s.id
When a database returns records by connecting two or more tables , Will generate a temporary table in the middle , Then the temporary table is returned to the user .
In the use of left jion when ,on and where The conditions differ as follows :
1、 on The condition is the condition used to generate the temporary table , It doesn't care on Is the condition in true , Will return to the records in the table on the left .
2、where The condition is that after the temporary table is generated , Then filter the temporary table . There is no such thing as left join The meaning of ( You have to go back to the records in the table on the left ) 了 , If the conditions are not true, filter them out .
Suppose there are two tables :
surface 1:tab2
id
size
1
10
2
20
3
30
surface 2:tab2
size
name
10
AAA
20
BBB
20
CCC
Two article SQL:
1、select * form tab1 left join tab2 on (tab1.size = tab2.size) where tab2.name='AAA'
2、select * form tab1 left join tab2 on (tab1.size = tab2.size and tab2.name='AAA')
Article 1 with a SQL The process of :
1、 In the middle of table
on Conditions :
tab1.size = tab2.size
tab1.idtab1.sizetab2.sizetab2.name
1
10
10
AAA
2
20
20
BBB
2
20
20
CCC
3
30
(null)
(null)
|
|
2、 Then filter the middle table
where Conditions :
tab2.name=’AAA’
tab1.idtab1.sizetab2.sizetab2.name
1
10
10
AAA
Second SQL The process of :
1、 In the middle of table
on Conditions :
tab1.size = tab2.size and tab2.name=’AAA’
( If the condition is not true, the record in the left table will be returned )
tab1.idtab1.sizetab2.sizetab2.name
1
10
10
AAA
2
20
(null)
(null)
3
30
(null)
(null)
In fact, the key reason for the above results is left join,right join,full join The particularity of , No matter on Whether the condition on is true or not will be returned left or right The records in the table ,full It has left and right The union of the properties of . and inner jion It's not that special , The conditions are as follows on neutralization where in , The result set returned is the same .
author :jcpp9527
blog.csdn.net/wqc19920906/article/details/79785424
边栏推荐
- Download and install mysql5.7 for windows 10
- day31 js笔记 DOM下 2021.09.26
- Docker modifies the user name and password of MySQL
- [sciter]:sciter如何使用i18实现桌面应用多语言切换及其利弊
- day33 js笔记 事件(下)2021.09.28
- Splicing strings in the string collection_ Stream based
- It is safer for individuals to choose which securities company to open an account for buying floor funds
- 2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
- Chapter 2 do you remember the point, line and surface (2)
- Making and using of dynamic library (shared library)
猜你喜欢
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
水果FL Studio/Cubase/Studio one音乐宿主软件对比
The default point of this in JS and how to modify it to 2021.11.09
day37 js笔记 运动函数 2021.10.11
This Exception was thrown from a job compiled with Burst, which has limited exception support. report errors
Day39 prototype chain and page fireworks effect 2021.10.13
Class pattern and syntax in JS 2021.11.10
《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
Making and using of static library
day36 js笔记 ECMA6语法 2021.10.09
随机推荐
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
什么是主链系统?
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
东方财富手机股票开户哪个券商更安全更方便?
Day23 JS notes 2021.09.14
Machine learning project captcha based on verification code recognition_ Trainer operation practice
Analyze whether there is duplicate data in the list and repeat it several times
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
2022中国信通院首届业务与应用安全发展论坛成功召开!
ThreadLocal的简单理解
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
day32 js笔记 事件(上)2021.09.27
功能真花哨,价格真便宜!长安全新SUV真实力到底怎样?
Word、PDF、TXT文件实现全文内容检索需要用什么方法?
QML控件类型:TabBar
GEE:基于 MCD64A1 的 GlobFire 日常火灾数据集
day24 js笔记 2021.09.15
day30 js笔记 BOM和DOM 2021.09.24