当前位置:网站首页>Mysql A left(right) join B on A.id=B.id and A.age=1与A left(right) join B on A.id=B.id where A.age=1
Mysql A left(right) join B on A.id=B.id and A.age=1与A left(right) join B on A.id=B.id where A.age=1
2022-07-22 21:27:00 【《@书山有径%》】
Mysql的join…on…and…与join…on…where…的区别
Mysql A left(right) join B on A.id=B.id and A.age=1与A left(right) join B on A.id=B.id where A.age=1的区别
建表和数据准备
创建左表和右表,以及建表语句和数据插入
create table tab_left(
id int,
name int,
age int,
sex int
);
insert into tab_left values(1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5,5,5,5),(6,6,6,6);
create table tab_right(
id int,
state int,
money int
);
insert into tab_right values(1,1,1),(2,2,2),(3,3,3),(4,4,4),(7,7,7),(8,8,8);
查看tab_left数据
select * from tab_left;

查看表tab_right数据
select * from tab_right;

内连接
两者无区别
通过id进行内连接,以及指定左表的name等于一个特定值,只能得到name等于特定值的记录。
select * from tab_left l join tab_right r on l.id=r.id where l.name=2;
select * from tab_left l join tab_right r on l.id=r.id and l.name=2;
结果一致
select * from tab_left l join tab_right r on l.id=r.id and r.state=8;
内连接后,8不包含,where过滤无记录,所以为null
左连接(右连接类似)
join...on....and... 在左连接(右连接)符合条件进行内连接,不符合进行外连接
join...on...where...,先进行外连接,在进行过滤
where 1
select * from tab_left l left join tab_right r on l.id=r.id where l.name=2;
先通过id进行外连接,再过滤name等于特定值的记录。
and 1
select * from tab_left l left join tab_right r on l.id=r.id and l.name=2;
如果只进行左连接,1到4应该进行内连接,5到6进行外连接,
加上name的连接条件就下 相当于 只对符合条件的进行内连接,其它使用外连接。只有2符合,进行内连接
where 2
select * from tab_left l left join tab_right r on l.id=r.id where r.state=3;
先通过id进行外连接,再过滤state等于特定值的记录。
and 2
select * from tab_left l left join tab_right r on l.id=r.id and r.state=3;
如果只进行左连接,1到4应该进行内连接,5到6进行外连接,
加上state的连接条件就下 相当于 只对符合条件的进行内连接,其它使用外连接。只有3符合,进行内连接
and 3
select * from tab_left l left join tab_right r on l.id=r.id and r.state=8;
如果只进行左连接,1到4应该进行内连接,5到6进行外连接,
加上state的连接条件就下 相当于 只对符合条件的进行内连接,其它使用外连接。没有符合条件的,所以右边全为null。
边栏推荐
- Alibaba Cloud Security Center's best practices for vulnerability repair
- Scala 当用到.contains() .exists()的性能问题
- Kubernetes 部署策略
- 一文深入浅出理解国产开源木兰许可系列协议
- Part I sourcetree installation
- Classes and objects (1)
- 关于Redis,是先更新数据库,还是先更新缓存?
- Scala学习——泛型[T]的6种使用
- Wechat campus second-hand book trading applet graduation design finished product (8) graduation design thesis template
- 2022年暑假ACM热身练习4(总结)
猜你喜欢

(五)数电——公式化简法

聊聊并发编程的12种业务场景

6-14漏洞利用-rpcbind漏洞利用

自定义flink es source
![[untitled] share an API Gateway project developed based on ABP vNext](/img/94/bfa4fa36e4974830e6afac118a593e.png)
[untitled] share an API Gateway project developed based on ABP vNext

Custom view: levitation ball and accelerator ball

多传感器融合综述---FOV与BEV

Report on the progress of writing Chinese experiments by latex

驱动页面性能优化的3个有效策略

Basic commands of redis' five basic data types
随机推荐
6-15漏洞利用-smb-RCE远程命令执行
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
Basic commands of redis' five basic data types
Classes and objects (1)
FTXUI基础笔记(hello world)
Application of workflow engine in vivo marketing automation
我是如何在一周内拿到4份offer的?
Small program completion work wechat campus second-hand book trading small program graduation design finished product (2) small program function
局域网SDN技术硬核内幕 9 从软件Overlay到硬件Overlay
Inside the hard core of LAN SDN technology - evpn implementation of 16 three from thing to person user roaming in the park
Why does MySQL index use b+ tree instead of jump table?
直播实录 | 37 手游如何用 StarRocks 实现用户画像分析
升级poi-tl版本1.12.0与旧版poi(4.1.2)、easyexcel之间的依赖冲突解决
局域网SDN硬核技术内幕 19 团结一切可以团结的力量
Redis——JedisConnectionException Could not get a resource from the pool
ETL工具(数据同步)
Trees and binary trees
ROS based navigation framework
BGP笔记(二)
Problems encountered in punching