当前位置:网站首页>在mysql中同时使用left join on 和where 的查询结果分析
在mysql中同时使用left join on 和where 的查询结果分析
2022-07-27 06:10:00 【hawanglc】
以前用惯了oracle,在处理左连接和有链接的时候,只需要在sql中添加(+)就可以了,但是在mysql中,将left join或者right join 与 on 和 where进行联合使用的时候,不同的联合用法,得到的却是不同的结果。现在记录一下。
假设有左表tb_oder,该表有order_id和user_id字段;有右表tb_user,该表有user_id和user_name两个字段,两个表使用user_id进行关联。
SQL1:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id;
这是最常规的用法,如果右表中没有对应的数据,则结果集中显示为空。
SQL2:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
and u.user_id > 10;
这个对右表进行筛选之后再与左表关联的,所以,即便右表和左表中都有user_id<10的数据,user_name的显示结果也为空。
SQL3
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
and o.order_id > 10;
SQL4:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
and o.user_id > 10;
这两个语句貌似是对左表进行筛选之后,再与右表关联,但是他并不是这样的。实际上,无论如何,左表tb_order中的数据都会全部显示,对左表进行限定的条件下,对应的右表中符合关联条件的内容却被强制设置为空。这两个语句的效果与SQL2是一样的,但是效果却难以理解,一般不建议这么写。
SQL5:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
where u.user_id > 10;
SQL6:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
where o.user_id > 10;
这两个语句的结果更有意思,他们的效果是一样的,就是,把使用left join on 进行关联的结果集再按照where条件进行筛选。
按照上面的分析,似乎,这样的SQL中,并无一个比较易懂的,首先去筛选左表的内容,然后再与右表进行匹配的办法。较好的办法就是,对左表用子查询进行筛选,如果无法理解上面的left join on where 中的where的使用方式,也可以对右表用子查询进行筛选。
SQL如下:
select o.order_id,o.user_id,u.user_id,u.user_name
from ( select * from tb_order where order_id > 10 ) o LEFT JOIN tb_user u
on o.user_id = u.user_id;
select o.order_id,o.user_id,u.user_id,u.user_name
from ( select * from tb_order where order_id > 10 ) o LEFT JOIN (select * from tb_user where user_id > 20) u
on o.user_id = u.user_id;
边栏推荐
- Pytorch model
- 使用popen来执行一个命令并获得返回结果
- 在rhel8上使用soci连接oracle和postgresql和sqlite
- C4D云渲染平台选哪家合作?
- 腾讯云服务器SSH链接自动断开解决方法
- (转帖)eureka、consul、nacos的对比1
- 算法--斐波那契数列(Kotlin)
- Automatically generate UML sequence diagram according to text (draw.io format)
- Drools(5):Drools高级语法
- Quartus: an error is reported when adding a.V file to someone else's project
猜你喜欢

Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践

35. Search insert position

端口转发小结

Bash: 创建返回布尔类型值的函数

Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.

pytorch笔记:TD3

(转帖)eureka、consul、nacos的对比2

Visual horizontal topic bug1:filenotfounderror: could not find module 'mvcameracontrol dll‘ (or one of it

使用pip命令切换不同的镜像源
随机推荐
ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
Which C4d cloud rendering platform to cooperate with?
Codeforces Round #809 (Div. 2)(6/6)(Kruskal重构树)
?实验 7 基于 Mysql 的 PHP 管理系统实现
二叉树--天然的查找语义(1)基础篇
36 - new promise method: allsettled & any & race
Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort
Gbase 8C product introduction
VIM editor deletes all file contents
Es compares the data difference between the two indexes
oracle的触发器的使用举例
What is OKR and what is the difference between OKR and KPI
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field
Vscode creates golang development environment and debug unit test of golang
SQLite 常用功能整合
整体二分?
pre-commit install 时 CalledProcessError
DDD Domain Driven Design Notes
Drools(5):Drools高级语法