当前位置:网站首页>阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
2022-06-28 10:51:00 【Java爱好狂.】
之前有码友去阿里面试,被问到 LEFT JOIN 关联表中用 ON 还是 WHERE 跟条件有什么区别,很快就答出来了,可是追问什么原因造成这一情况的,一时没回答上来。
下面说说,想通过 A left B join on and 后面的条件来使查出的两条记录变成一条,奈何发现还是有两条。
后来发现 join on and 不会过滤结果记录条数,只会根据and后的条件是否显示 B表的记录,A表的记录一定会显示。
不管and 后面的是A.id=1还是B.id=1,都显示出A表中所有的记录,并关联显示B中对应A表中id为1的记录或者B表中id为1的记录。
运行sql :
select * from student s left join class c on s.classId=c.id order by s.id
运行sql :
select * from student s left join class c on s.classId=c.id and s.name="张三" order by s.id
运行sql :
select * from student s left join class c on s.classId=c.id and c.name="三年级三班" order by s.id
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
在使用left jion时,on和where条件的区别如下:
1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
假设有两张表:
表1:tab2
id
size
1
10
2
20
3
30
表2:tab2
size
name
10
AAA
20
BBB
20
CCC
两条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')第一条SQL的过程:
1、中间表
on条件:
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、再对中间表过滤
where 条件:
tab2.name=’AAA’
tab1.idtab1.sizetab2.sizetab2.name
1
10
10
AAA
第二条SQL的过程:
1、中间表
on条件:
tab1.size = tab2.size and tab2.name=’AAA’
(条件不为真也会返回左表中的记录)
tab1.idtab1.sizetab2.sizetab2.name
1
10
10
AAA
2
20
(null)
(null)
3
30
(null)
(null)
其实以上结果的关键原因就是left join,right join,full join的特殊性,不管on上的条件是否为真都会返回left或right表中的记录,full则具有left和right的特性的并集。而inner jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。
作者:jcpp9527
blog.csdn.net/wqc19920906/article/details/79785424
边栏推荐
- JSON模块、hashlib、base64
- 如何使用 DataAnt 监控 Apache APISIX
- MySQL(一)
- 爱可可AI前沿推介(6.28)
- Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)
- Must the MySQL table have a primary key for incremental snapshots?
- MySQL (II)
- Redis database
- [Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
- idea连接sql sever失败
猜你喜欢
![[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)](/img/02/4dbd97c8b8df1c96b7c8e1460aeda2.png)
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)

ruoyi集成积木报表(nice)

How to use dataant to monitor Apache apisex

第六天 脚本与动画系统

Katalon当中的debug调试

The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached

MySQL(一)

Spatial-Temporal时间序列预测建模方法汇总

【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)

Dataease installation upgrade
随机推荐
MytipartFile与File的相互转换
Katalon当中的debug调试
June training (day 28) - Dynamic Planning
Realization of a springboard machine
fastposter v2.8.4 发布 电商海报生成器
JSON模块、hashlib、base64
MySQL cannot be opened. Flash back
知道 Redis RDB 这些细节,可以少踩很多坑
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
Dataease installation upgrade
Realization of a springboard machine
DlhSoft Kanban Library for WPF
Transactions proof in appliedzkp zkevm (10)
一种跳板机的实现思路
Interface automation framework scaffolding - Implementation of parametric tools
Mysql database overview and installation process
How does ETF position affect spot gold price?
Datetime and logging module
【Qt】connect 语法参考实现
flink1.15,支持mysql视图吗?我这边在table-name处配置视图名保存,找不到表。想