当前位置:网站首页>数据库问题汇总
数据库问题汇总
2022-07-02 04:48:00 【一盏红烛,一杯烧酒】
left join on and vs where
场景描述
查询业务数据是,有如下需求。需要从
a表中去除b表中包含的数据。我的第一个想法是a left join b on a.id = b.aid and b.aid is null,然后数据是出来了。但是抽了几个发现,a中有,b中也有。于是我陷入了沉思。
复现
造数据
CREATE TABLE `join_1` (
`id` int(11) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `join_2` (
`id` int(11) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`thing` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


查询
select * from join_1 left join join_2 on join_1.id = join_2.id and join_2.phone is not null;
以and查询结果如下

这里很明显可以看到,我明明不想要手机号为
null的,但是查询结果里怎么还是有。。我明明加了判空条件了。。
select * from join_1 left join join_2 on join_1.id = join_2.id where join_2.phone is not null;
以where查询结果如下

这里的查询结果正是我想要的。但是
and和where有啥区别呢。
分析
where语句发生在join之后,它是对join结果的过滤on、and语句,发生在join之前,数据在join的过程中被过滤。
这里我用and查询存在空值是因为,join之前join_2表中的确有数据,后面left join之后才产生null数据的。
边栏推荐
- Practical problem solving ability of steam Education
- 汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
- Summary of main account information of zhengdaliu 4
- Unit testing classic three questions: what, why, and how?
- Learn what definitelytyped is through the typescript development environment of SAP ui5
- Leetcode merge sort linked list
- 二叉树解题(一)
- What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
- 正大美欧4的主账户关注什么数据?
- Realize the function of data uploading
猜你喜欢

How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer

CY7C68013A之keil编译代码

Idea automatic package import and automatic package deletion settings

Gin framework learning code

解决:代理抛出异常错误

关于Steam 教育的知识整理

Idea autoguide package and autodelete package Settings

解析少儿编程中的动手搭建教程

MySQL table insert Chinese change? Solution to the problem of No
![[C language] basic learning notes](/img/d2/1aeb2d37d97b9cfe4b21aa3ac37645.png)
[C language] basic learning notes
随机推荐
The solution to the complexity brought by lambda expression
数学知识——快速幂的理解及例题
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
Why can't you remember when reading? Why can't you remember- My technology learning methodology
洛谷入门3【循环结构】题单题解
Record the bug of unity 2020.3.31f1 once
06 装饰(Decorator)模式
Pytoch --- use pytoch to realize u-net semantic segmentation
Use of Baidu map
Cannot activate CONDA virtual environment in vscode
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
win11安装pytorch-gpu遇到的坑
Keil compilation code of CY7C68013A
LeetCode-归并排序链表
关于Steam 教育的知识整理
二叉树解题(一)
Deep understanding of lambda expressions
6月书讯 | 9本新书上市,阵容强大,闭眼入!
[understand one article] FD_ Use of set