当前位置:网站首页>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
记录一下,总结经验
边栏推荐
- 首份中国企业敏捷实践白皮书发布| 附完整下载
- 【深入浅出玩转FPGA学习4----漫谈状态机设计】
- JVM garbage collector
- 华为应用市场应用统计数据问题大揭秘
- Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
- 6种单例模式的实现方式
- HDU1236 排名(结构体排序)
- 二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
- 二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
- Use of vscode tool
猜你喜欢

Hdu1236 ranking (structure Sorting)

From Read and save in bag file Jpg pictures and PCD point cloud
![[AI application] Hikvision ivms-4200 software installation](/img/4e/1640e3cafac13ce4d39520195c3217.png)
[AI application] Hikvision ivms-4200 software installation

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)

Disassembling Meitu SaaS: driving the plane to change the engine

What are the software product management systems? Inventory of 12 best product management tools

三.芯片啟動和時鐘系統
![[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?](/img/66/674a06d8e45a31ae879b81554ef373.png)
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?

首份中国企业敏捷实践白皮书发布| 附完整下载
随机推荐
Nodejs+express+mysql simple blog building
华为联机对战服务玩家掉线重连案例总结
Iii. Système de démarrage et d'horloge à puce
快应用中实现自定义抽屉组件
[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
华为游戏初始化init失败,返回错误码907135000
TIPC Getting Started6
Special topic of binary tree -- [deep base 16. Example 7] ordinary binary tree (simplified version) (multiset seeks the precursor and subsequent sentry Art)
What are the software product management systems? Inventory of 12 best product management tools
[AGC] build service 3 - authentication service example
Special topic of binary tree -- Logu p1229 traversal problem (the number of traversals in the middle order is calculated when the pre and post order traversals of the multiplication principle are know
2022-06-17
P1055 [noip2008 popularization group] ISBN number
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
TIPC messaging3
Open the encrypted SQLite method with sqlcipher
One trick to quickly realize custom application titlebar
HDU1234 开门人和关门人(水题)