读书人

一个SQL语句,该如何解决

发布时间: 2013-10-21 17:03:30 作者: rapoo

一个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代表表中有多少条表记录。

读书人网 >SQL Server

热点推荐