当前位置:网站首页>数据库问题汇总
数据库问题汇总
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
数据的。
边栏推荐
- Gin framework learning code
- Realize the function of data uploading
- 初学爬虫-笔趣阁爬虫
- Mysql database learning
- Rhcsa --- work on the fourth day
- There is no prompt for SQL in idea XML, and the dialect setting is useless.
- Solution of DM database unable to open graphical interface
- 社交媒体搜索引擎优化及其重要性
- Federal learning: dividing non IID samples according to Dirichlet distribution
- geotrust ov多域名ssl证书一年两千一百元包含几个域名?
猜你喜欢
Summary of main account information of zhengdaliu 4
VMware installation win10 reports an error: operating system not found
Introduction to Luogu 3 [circular structure] problem list solution
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Online incremental migration of DM database
正大美欧4的主账户关注什么数据?
C language practice - number guessing game
Cannot activate CONDA virtual environment in vscode
win11安装pytorch-gpu遇到的坑
Landing guide for "prohibit using select * as query field list"
随机推荐
6.30年终小结,学生时代结束
Binary tree problem solving (2)
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
Design and implementation of general interface open platform - (44) log processing of API services
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
oracle 存储过程与job任务设置
ansible安装与使用
Mysql中常见的锁
Federal learning: dividing non IID samples according to Dirichlet distribution
Embedded-c language-9-makefile/ structure / Consortium
Cannot activate CONDA virtual environment in vscode
Application of intelligent robot in agricultural ecology
Use of typescript classes
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
There is no prompt for SQL in idea XML, and the dialect setting is useless.
Summary of common string processing functions in C language
正大留4的主账户信息汇总
idea自動導包和自動删包設置
记录一次Unity 2020.3.31f1的bug