当前位置:网站首页>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
记录一下,总结经验
边栏推荐
- TIPC Service and Topology Tracking4
- MySQL lethal serial question 3 -- are you familiar with MySQL locks?
- Open the encrypted SQLite method with sqlcipher
- 最详细MySql安装教程
- Why does LabVIEW lose precision in floating point numbers
- 全网显示 IP 归属地,是怎么实现的?
- 【AppLinking实战案例】通过AppLinking分享应用内图片
- static 函数中的静态变量
- Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
- 二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
猜你喜欢
TIPC 寻址2
TIPC messaging3
2022爱分析· 国央企数字化厂商全景报告
一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
Open the encrypted SQLite method with sqlcipher
TIPC Service and Topology Tracking4
HDU1234 开门人和关门人(水题)
What are the software product management systems? Inventory of 12 best product management tools
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
TIPC Cluster5
随机推荐
[ark UI] implementation of the startup page of harmonios ETS
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
P1055 [NOIP2008 普及组] ISBN 号码
TIPC 寻址2
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
MySQL keyword
快应用中实现自定义抽屉组件
Flink two Open, implement Batch Lookup join (attached source)
三.芯片启动和时钟系统
V2X-Sim数据集(上海交大&纽约大学)
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
Special topic of binary tree -- [deep base 16. Example 7] ordinary binary tree (simplified version) (multiset seeks the precursor and subsequent sentry Art)
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
从.bag文件中读取并保存.jpg图片和.pcd点云
正则及常用公式
Matlab processing of distance measurement of experimental electron microscope
Why does LabVIEW lose precision in floating point numbers
【付费推广】常见问题合集,推荐榜单FAQ
Indexer in C #
Is the account above changtou school safe?