当前位置: 代码迷 >> Sql Server >> 一个SQL语句,该如何解决
  详细解决方案

一个SQL语句,该如何解决

热度:53   发布时间:2016-04-25 00:02:29.0
一个SQL语句
表T1、T2
T1.f1 和 T2.f2 关联

我想查找 T1中, (T1.f1 = T2.f2 )以外的记录,就是找出T1中没有关联上的语句,请问该怎么写?

------解决方案--------------------
select * from t1 a where not exists(select 1 from t2 where a.f1=f2)

------解决方案--------------------

select a.*
from T1 a
left join T2 b on a.f1=b.f2 
where b.f2 is null

------解决方案--------------------
引用:
引用:
SQL code

select * from t1 a where not exists(select 1 from t2 where a.f1=f2)


SQL code

select 1 from t2 where a.f1=f2

对这句话不怎么理解,select 1  是什么意思?


1 是一个常量 , 如果表中有值,那么查询出来的结果集合有多少个1代表表中有多少条表记录。
  相关解决方案