随笔 - 832  文章 - 2  评论 - 31  阅读 - 167万

left join on 后跟and和where区别

复制代码
create table testA(
  id int primary key,
  Name varchar(10)
);
insert into testA values(1, '小黄');
insert into testA values(2, '小绿'); 
insert into testA values(3, '小白');
insert into testA values(4, '小黑');
insert into testA values(5, '小花');
commit;

create table testB(
  id int primary key, 
  age int(10)
);
insert into testB values(1, 10);
insert into testB values(2, 11);
insert into testB values(3, 12);
insert into testB values(4, 13);
复制代码

内连接

select * from testA t1 inner join testB t2 on t1.id=t2.id and t1.name='小白';

select * from testA t1 inner join testB t2 on t1.id=t2.id  where t1.name='小白';

 左连接

select * from testA t1 left join testB t2 on t1.id=t2.id and t1.name='小白';

select * from testA t1 left join testB t2 on t1.id=t2.id  where t1.name='小白';

其实以上结果的关键原因就是1eft join,right join,fulljoin的特殊性,不管on上的条件是否为真都会返回1eft或right表中的记录,ful1则具有1eft和right的特性的并集。而inner join 没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。on为了反映外连接中一方的全连接,而where没有这个功能,内连接配对是可以的。

posted on   小破孩楼主  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2021-03-17 jasypt 对 配置文件密码进行加密处理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示