当前位置:网站首页>数据库问题汇总
数据库问题汇总
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
数据的。
边栏推荐
- 農業生態領域智能機器人的應用
- I sorted out some basic questions about opencv AI kit.
- 二叉樹解題(二)
- Application of intelligent robot in agricultural ecology
- Record the bug of unity 2020.3.31f1 once
- Pytoch --- use pytoch to realize u-net semantic segmentation
- Vmware安装win10报错:operating system not found
- What methods should service define?
- Rhcsa --- work on the third day
- 数学问题(数论)试除法做质数的判断、分解质因数,筛质数
猜你喜欢
Unity particle Foundation
My first experience of shadowless cloud computer
Markdown edit syntax
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
The core idea of performance optimization, dry goods sharing
数学知识——快速幂的理解及例题
关于Steam 教育的知识整理
缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
数学知识(欧拉函数)
随机推荐
【ClickHouse】How to create index for Map Type Column or one key of it?
Common errors of dmrman offline backup
解析少儿编程中的动手搭建教程
Use of Baidu map
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Gin framework learning code
Lay the foundation for children's programming to become a basic discipline
Learn what definitelytyped is through the typescript development environment of SAP ui5
二叉树解题(二)
Hcip day 17
二叉樹解題(二)
LM09丨费雪逆变换反转网格策略
6.30年终小结,学生时代结束
Mysql表insert中文变?号的问题解决办法
C language practice - number guessing game
Online incremental migration of DM database
Leetcode merge sort linked list
Learn BeanShell before you dare to say you know JMeter
Comp 250 parsing
Getting started with pytest -- description of fixture parameters