当前位置:网站首页>数据库问题汇总
数据库问题汇总
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
数据的。
边栏推荐
- Mysql database learning
- oracle 存储过程与job任务设置
- Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
- Practical problem solving ability of steam Education
- Typescript function details
- 数学知识——快速幂的理解及例题
- Idea autoguide package and autodelete package Settings
- Mouse events in JS
- 10 minute quick start UI automation ----- puppeter
- Embedded-c language-9-makefile/ structure / Consortium
猜你喜欢
Practical problem solving ability of steam Education
社交媒体搜索引擎优化及其重要性
CY7C68013A之keil编译代码
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Markdown编辑语法
Leetcode merge sort linked list
Cannot activate CONDA virtual environment in vscode
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
面试会问的 Promise.all()
Promise all()
随机推荐
I sorted out some basic questions about opencv AI kit.
Starting from the classification of database, I understand the map database
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
Binary tree problem solving (2)
Application of intelligent robot in agricultural ecology
记录一次Unity 2020.3.31f1的bug
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
解析少儿编程中的动手搭建教程
Idea autoguide package and autodelete package Settings
二叉树解题(一)
Read "the way to clean code" - function names should express their behavior
面试会问的 Promise.all()
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
Getting started with pytest -- description of fixture parameters
Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
[improvement class] st table to solve the interval maximum value problem [2]
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
解决:代理抛出异常错误
Ognl和EL表达式以及内存马的安全研究
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)