当前位置:网站首页>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
记录一下,总结经验
边栏推荐
- [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
- 【AI应用】海康威视iVMS-4200软件安装
- AppGallery Connect场景化开发实战—图片存储分享
- Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
- How to implement tabbar title bar with list component
- 二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
- Indexer in C #
- Luogu p4281 [ahoi2008] emergency gathering / gathering (tree doubling LCA)
- 三.芯片启动和时钟系统
- Jenkins安装
猜你喜欢

华为快应用中如何实现同时传递事件对象和自定义参数

Jsp webshell Free from killing - The Foundation of JSP

Read H264 parameters from mediarecord recording

2022-06-17

One trick to quickly realize custom application titlebar

TIPC Service and Topology Tracking4

Uncover the secrets of Huawei application market application statistics

V2x SIM dataset (Shanghai Jiaotong University & New York University)

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

Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
随机推荐
Primary key policy problem
Flick two open, realized a batch lookup join (with source code)
static 函数中的静态变量
How to use ide to automatically sign and debug Hongmeng application
[in simple terms, play with FPGA learning 3 ----- basic grammar]
2022-06-17
洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
使用华为性能管理服务,按需配置采样率
【深入浅出玩转FPGA学习3-----基本语法】
Analysis of hot spots in AI technology industry
Oracle 笔记
洛谷 P3398 仓鼠找 sugar(树上倍增 lca 判断树中两条路径是否相交 结论)
Binary tree topic -- p1030 [noip2001 popularization group] find the first order
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
MySQL environment configuration
Calculate the sum of sequences
Read H264 parameters from mediarecord recording
Jsp webshell Free from killing - The Foundation of JSP
二叉树专题--AcWing 1589. 构建二叉搜索树