当前位置:网站首页>sql left join 主表限制条件写在on后面和写在where后面的区别
sql left join 主表限制条件写在on后面和写在where后面的区别
2022-07-02 07:41:00 【不吃天鹅肉】
如题,假设有这么一道sql题
with a as (
select 1 as id , 'a' as name
union all
select 1,'b'
),
b as (
select 1 as id1, 'q' as name1
union all
select 1 , 'w'
)
select count(*) from (
select *,'a' as num
from a
left join b
on a.id = b.id1
and a.name = 'a'
问答案是几?
想必大部分人都会回答 2 ,至少我是这样的。在我原先的理解中,这个sql的执行顺序应该是这样的
- 执行关联,这是会出现四条结果,数据为 1 a 1 q; 1 a 1 w; 1 b 1 q; 1 b 1 w;
- 执行过滤,去掉a.name <>'a’的,
最后结果为 1 a 1 q; 1 a 1 w;
但这样是错的!!!
对于这道sql来说,执行顺序应该是这样的。
- 首先检测到left join ,直接返回a表全部结果,结果为 1 a ;1 b
- 拿a表符合 a.name = 'a’的,去与b表进行关联,不符合条件的,b表置为null,结果为 1 a 1 q; 1 a 1 w; 1 b null null
所以,结果为3
记录一下,总结经验
边栏推荐
- From Read and save in bag file Jpg pictures and PCD point cloud
- 首份中国企业敏捷实践白皮书发布| 附完整下载
- Hdu1236 ranking (structure Sorting)
- 全网显示 IP 归属地,是怎么实现的?
- 二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
- 快应用中实现自定义抽屉组件
- Appgallery connect scenario development practice - image storage and sharing
- TIPC protocol
- QT learning diary 8 - resource file addition
- 三.芯片啟動和時鐘系統
猜你喜欢

TIPC 寻址2

【AI应用】海康威视iVMS-4200软件安装
![[applinking practical case] share in app pictures through applinking](/img/12/5616f1fa55387b81e25e55a98022b9.png)
[applinking practical case] share in app pictures through applinking

Nodejs+express+mysql simple blog building

OpenMLDB Meetup No.4 会议纪要

Hdu1236 ranking (structure Sorting)

Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)

实验电镜距离测量之Matlab处理

JSP webshell free -- the basis of JSP

如何用list组件实现tabbar标题栏
随机推荐
使用华为性能管理服务,按需配置采样率
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
Set the playback speed during the playback of UOB equipment
[quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched
III Chip startup and clock system
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
php中self和static在方法中的区别
二叉树专题--AcWing 1589. 构建二叉搜索树
学习open62541 --- [66] UA_String的生成方法
PCL point cloud to depth image
Binary tree topic -- p1030 [noip2001 popularization group] find the first order
One trick to quickly realize custom application titlebar
How to transfer event objects and user-defined parameters simultaneously in Huawei express applications
【AppLinking实战案例】通过AppLinking分享应用内图片
JSP webshell免殺——JSP的基礎
[AGC] build service 3 - authentication service example
Implement custom drawer component in quick application
Creation and use of unified links in Huawei applinking
QT learning diary 7 - qmainwindow