当前位置:网站首页>Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
2022-07-02 09:07:00 【Programmer Qiqi】
Code friends went to jd.com for an interview before , 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 .
边栏推荐
- 【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
- Installing Oracle database 19C for Linux
- How to realize asynchronous programming in a synchronous way?
- commands out of sync. did you run multiple statements at once
- Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
- gocv图片裁剪并展示
- commands out of sync. did you run multiple statements at once
- 【Go实战基础】gin 如何绑定与使用 url 参数
- Connect function and disconnect function of QT
- Matplotlib剑客行——容纳百川的艺术家教程
猜你喜欢
QT -- how to set shadow effect in QWidget
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Service de groupe minecraft
Application of kotlin - higher order function
Kubernetes deploys Loki logging system
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
oracle修改数据库字符集
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
MYSQL安装出现问题(The service already exists)
京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
随机推荐
AMQ6126问题解决思路
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
C4D quick start tutorial - C4d mapping
Judge whether it is Sudoku
Finishing the interview essentials of secsha system!!!
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
一个经典约瑟夫问题的分析与解答
win10使用docker拉取redis镜像报错read-only file system: unknown
Move a string of numbers backward in sequence
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
查看was发布的应用程序的端口
Installing Oracle database 19C RAC on Linux
QT qtimer class
Dip1000 runaway
Oracle修改表空间名称以及数据文件
Oracle 相关统计
Select sort and insert sort
【Go实战基础】gin 如何验证请求参数
Luogu greedy part of the backpack line segment covers the queue to receive water