加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 当表左连接时,where子句和on子句有什么区别?

发布时间:2020-12-12 16:42:17 所属栏目:MsSql教程 来源:网络整理
导读:SQL1: select t1.f1,t2.f2 from t1 left join t2 on t1.f1 = t2.f2 and t1.f2=1 and t1.f3=0 SQL2: select t1.f1,t2.f2 from t1 left join t2 on t1.f1 = t2.f2 where t1.f2=1 and t1.f3=0 区别在于and on子句,是否有相同的返回结果?有什么区别? DBMS是否
SQL1:
select t1.f1,t2.f2 
from t1 
   left join t2 on t1.f1 = t2.f2 and t1.f2=1 and t1.f3=0

SQL2:

select t1.f1,t2.f2 
from t1 
  left join t2 on t1.f1 = t2.f2 
where t1.f2=1 and t1.f3=0

区别在于and on子句,是否有相同的返回结果?有什么区别? DBMS是否以相同的方式运行它们?谢谢.

解决方法

where子句适用于整个结果集; on子句仅适用于有问题的联接.

在提供的示例中,与连接内侧上的字段相关的所有附加条件 – 因此在本示例中,两个查询是有效相同的.

但是,如果您在连接外侧的表格中包含条件,则会有显着差异.

您可以从以下链接获得更多信息:http://ask.sqlservercentral.com/questions/80067/sql-data-filter-condition-in-join-vs-where-clause

例如:

select t1.f1,t2.f2 from t1 left join t2 on t1.f1 = t2.f2 and t2.f4=1

select t1.f1,t2.f2 from t1 left join t2 on t1.f1 = t2.f2 where t2.f4=1

– 做不同的事情 – 前者将加入t2记录,其中f4是1,而后者已经被有效地转回到内部连接到t2.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读