left join on and |where 的区别
2020-04-23 11:25
453 查看
例如存在两张表结构
表结构1
- drop table if EXISTS A;
- CREATE TABLE A (
- ID int(1) NOT NULL,
- PRIMARY KEY (ID)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
表结构2
- drop table if EXISTS B;
- CREATE TABLE B (
- ID int(1) NOT NULL,
- PRIMARY KEY (ID)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
表一插入数据
- insert into A values ( 1 );
- insert into A values ( 2 );
- insert into A values ( 3 );
- insert into A values ( 4 );
- insert into A values ( 5 );
- insert into A values ( 6 );
表二插入数据
- insert into B values ( 1 );
- insert into B values ( 2 );
- insert into B values ( 3 );
完成后A,B表数据如下:
语句一
select A.ID as AID, B.ID as BID from A left join B on A.ID = B.ID where B.ID<3
语句二
select A.ID as AID, B.ID as BID from A left join B on A.ID = B.ID and B.ID<3
[/code]
语句一的查询结果
语句二的查询结果为:
从结果可见这两个语句存在差异,为什么会有差异的,比较适合我的理解是:
left join on where :是先left join 再 where
select * from A left join B on A.ID = B.ID and B.ID<3 :是先对B进行过滤再进行left joIn,再以A为主表,返回数据。相当于要对A表和B表联合展示,A为主表,B在关联前需要满足多个条件。
转载于:https://www.cnblogs.com/SchrodingersCat/p/leftjoin.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 关于数据库中使用 left join on ...and ...和 left join on ....where ...区别,和使用group by 要注意的情况
- left join on and 与 left join on where的区别
- left join on and与left join on where的区别
- left join on and 与 left join on where的区别
- left join on and 与 left join on where的区别
- left join on and 与 left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- 左连接 A left join B on A.a = B.a and/where的区别
- left join on and 与 left join on where的区别
- left join on and与left join on where的区别
- left join on and 与 left join on where的区别
- left join on and与left join on where的区别
- [转载]SQL语句中LEFT JOIN ON WHERE和LEFT JOIN ON AND的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- 转!!left join on and 与 left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别