2

我正在尝试从RepDailyInfo表中获取 DatesRepDailyCollection.IsReceived = 0以及何时RepDailyCollection确实有该特定的记录RepDailyInfoID。这在 sql server 2005 中可能吗?

Select distinct RepDailyInfo.Date 
from RepDailyInfo 
left outer join RepDailyCollection c 
    on c.RepDailyInfoID = RepDailyInfo.RepDailyInfoID
where c.IsReceived = 0 
    or c.IsReceived = null
4

4 回答 4

1

将条件移动到ON连接的子句中,否则您需要连接来为WHERE子句中要引用的行找到一行:

Select distinct RepDailyInfo.Date 
from RepDailyInfo 
left outer join RepDailyCollection c 
    on c.RepDailyInfoID = RepDailyInfo.RepDailyInfoID
    and (c.IsReceived = 0 or c.IsReceived is null)

还要注意or在 SQL 中需要使用括号,因为“或”优先于“和”,如果没有括号,您最终会得到“(A 和 B)或 C”

于 2012-04-11T20:01:00.810 回答
0

你可能想做

where IsNull(c.IsReceived,0) = 0
于 2012-04-11T20:00:35.240 回答
0

是的,但是您必须在 join 本身中过滤 join 的右侧:

Select distinct RepDailyInfo.Date 
from RepDailyInfo 
left outer join RepDailyCollection c 
    on c.RepDailyInfoID = RepDailyInfo.RepDailyInfoID
   and (c.IsReceived = 0 
    or c.IsReceived is null)
于 2012-04-11T20:00:41.030 回答
0

WHERE子句在OUTER JOIN. 如果您不想要这种行为,请将WHERE子句移到 的ON子句中JOIN,如下所示:

select distinct r.date
from RepDailyInfo r
left outer join RepDailyCollection c on c.RepDailyInfoID = r.RepDailyInfoID
    and isnull(c.IsReceived, 0) = 0
于 2012-04-11T20:00:43.707 回答