1

我想知道我对这些运营商做错了什么。

我最终试图获得一份缺少 p.TEST 或 p.DL 之一的人的名单,但不是他们都在同一个人身上。

 select ....
  from ....

    WHERE

 ((p.TEST is null OR p.DL is null) OR
 (p.TEST = 0 or p.DL = 0) OR
 (p.TEST = 1 or p.DL = 0) OR
 (p.TEST = 0 or p.DL = 1))
4

3 回答 3

1

但不是他们两个在同一个人身上。

您的陈述中不保证这一点。

 ((p.TEST is null OR p.DL is null) OR  // if both are null this is true
 (p.TEST = 0 or p.DL = 0) OR           // if both are zero this is true
 (p.TEST = 1 or p.DL = 0) OR
 (p.TEST = 0 or p.DL = 1))

您必须明确检查:

((a is null) and (b is not null)) OR
((b is null) and (a is not null))
// etc. for zero values:
((a = 0) and (b  <> 0)) OR
((b = 0) and (a <> 0))
//...

即使这样,您也会得到类似的结果,a = 0但是b = null您可能也需要过滤这些结果。

于 2013-03-25T14:58:56.707 回答
1
select ..
from ..
where 
    -- one missing 
    (isnull(p.test,0) = 0 or isnull(p.dl,0) = 0
    and 
    -- but not both
    not (isnull(p.test,0) = 0 and isnull(p.dl,0) = 0)
于 2013-03-25T15:00:07.750 回答
0

我认为应该是

 ( 
   (p.TEST is null AND p.DL is not null) OR 
   (p.TEST is not null AND p.DL is null)
 )

...等等

这是因为您想知道一个人何时出现而另一个人何时失踪

于 2013-03-25T14:59:22.930 回答