【发布时间】:2012-12-09 02:10:26
【问题描述】:
我很困惑为什么这不起作用,我已经多次使用这种类型的语法,但这个语法让我大吃一惊。
我有两张桌子;
tableA
regId name regStatus
1 George 1
2 Jenny 1
3 Penny 1
4 James 1
5 Preston 1
6 Jamie 0
表B
activeRegId passiveRegID Status
1 2 1
1 3 1
1 4 0
6 1 1
我要做的是从tableA 返回所有行,不包括(tableA.regstatus = 0) 和(tableB.status = 1 for a user regid = 1) 的行。
我想避免使用NOT IN (select ...)。
到目前为止我的查询:
select top 10
tA.regId, tA.name
from
tableA tA
left OUTER JOIN
tableB tB ON tB.activeRegId = tA.regid AND tB.passiveRegID <> 1
AND tB.status <> 1 AND tB.passiveRegID IS NULL
where
tA.regStatus = 1
and tA.regid <> 1
我所期待的应该如下,但是,除了 Jamie 之外,我将获得 tableA 中的所有用户。
regId name
4 James
5 Preston
【问题讨论】:
标签: sql sql-server-2005 outer-join