当前位置:网站首页>数据库问题汇总
数据库问题汇总
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数据的。
边栏推荐
- Embedded-c language-9-makefile/ structure / Consortium
- Realize the function of data uploading
- DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
- How muddy is the water in the medical beauty industry with a market scale of 100 billion?
- 06 装饰(Decorator)模式
- Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
- Let genuine SMS pressure measurement open source code
- Introduction to Luogu 3 [circular structure] problem list solution
- 培养中小学生对教育机器人的热爱之心
- 洛谷入门3【循环结构】题单题解
猜你喜欢
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution

Pit encountered in win11 pytorch GPU installation

阿里云polkit pkexec 本地提权漏洞

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

Application d'un robot intelligent dans le domaine de l'agroécologie

Idea automatic package import and automatic package deletion settings

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

Mysql表insert中文变?号的问题解决办法

Starting from the classification of database, I understand the map database

How to modify data file path in DM database
随机推荐
Design and implementation of general interface open platform - (44) log processing of API services
Arbre binaire pour résoudre le problème (2)
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
ansible安装与使用
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
奠定少儿编程成为基础学科的原理
The solution to the complexity brought by lambda expression
CY7C68013A之keil编译代码
面试会问的 Promise.all()
Orthogonal test method and function diagram method for test case design
cs架构下抓包的几种方法
Beginner crawler - biqu Pavilion crawler
Promise all()
How to modify data file path in DM database
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
解决:代理抛出异常错误
農業生態領域智能機器人的應用
Ognl和EL表达式以及内存马的安全研究
Binary tree problem solving (1)
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?