SQL中的“on ..”和“on .. where”之间的区别加入?

前端之家收集整理的这篇文章主要介绍了SQL中的“on ..”和“on .. where”之间的区别加入?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
sql语句.
1.select a.* from A a left join B b on a.id =b.id and a.id=2;

2.select a.* from A a left join B b on a.id =b.id where a.id=2;

这两个sql语句有什么区别?

解决方法

create table A(id int);
create table B(id int);

INSERT INTO A VALUES(1);
INSERT INTO A VALUES(2);
INSERT INTO A VALUES(3);

INSERT INTO B VALUES(1);
INSERT INTO B VALUES(2);
INSERT INTO B VALUES(3);

SELECT * FROM A;
SELECT * FROM B;

id
-----------
1
2
3

id
-----------
1
2
3

过滤JOIN以防止在JOIN进程期间添加行.

select a.*,b.*
from   A a left join B b 
on     a.id =b.id and a.id=2;

id          id
----------- -----------
1           NULL
2           2
3           NULL

JOIN发生后,将过滤.

select a.*,b.* 
from   A a left join B b 
on     a.id =b.id 
where  a.id=2;

id          id
----------- -----------
2           2
原文链接:/mssql/82855.html

猜你在找的MsSQL相关文章