当前位置:网站首页>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
记录一下,总结经验
边栏推荐
- 一招快速实现自定义快应用titlebar
- Read H264 parameters from mediarecord recording
- Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
- QT学习日记8——资源文件添加
- 全网显示 IP 归属地,是怎么实现的?
- Filtering of PCL
- Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
- Use Huawei performance management service to configure the sampling rate on demand
- Implementation of six singleton modes
- Iii. Système de démarrage et d'horloge à puce
猜你喜欢

Use of vscode tool

从.bag文件中读取并保存.jpg图片和.pcd点云

MySQL environment configuration
![[applinking practical case] share in app pictures through applinking](/img/12/5616f1fa55387b81e25e55a98022b9.png)
[applinking practical case] share in app pictures through applinking

How to transfer event objects and user-defined parameters simultaneously in Huawei express applications

Jsp webshell Free from killing - The Foundation of JSP

最详细MySql安装教程
![[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?

快应用中实现自定义抽屉组件

HDU1228 A + B(map映射)
随机推荐
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
Set the playback speed during the playback of UOB equipment
Leetcode 182 Find duplicate email (2022.07.01)
三.芯片啟動和時鐘系統
I STM32 development environment, keil5/mdk5.14 installation tutorial (with download link)
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
计算序列之和
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
华为快应用中如何实现同时传递事件对象和自定义参数
Static variables in static function
软件产品管理系统有哪些?12个最佳产品管理工具盘点
Oracle 笔记
Summary of cases of players' disconnection and reconnection in Huawei online battle service
【深入浅出玩转FPGA学习2----设计技巧(基本语法)】
JSP webshell free -- webshell free
Jsp webshell Free from killing - The Foundation of JSP
[play with FPGA learning 5 in simple terms ----- reset design]
static 函数中的静态变量
How to transfer event objects and user-defined parameters simultaneously in Huawei express applications