7

我坐在一个逻辑问题上,我只是越来越困惑自己。

示例表

身份证 | 客户 ID | 任务 | 已确认 | 取消 | 完全的

1 | 20 | 正文 | 1 | 0 | 0
2 | 20 | 正文 | 1 | 1 | 0
3 | 20 | 正文 | 1 | 0 | 1
4 | 17 | 正文 | 0 | 1 | 0
5 | 17 | 正文 | 1 | 0 | 0
6 | 20 | 正文 | 1 | 空 | 空
7 | 20 | 正文 | 0 | 1 | 空值

我希望选择 custID = 20 的所有任务并确认 = 1。如果任务取消=1或完成=1,则不返回记录。

在这种情况下,返回的行将是 1 和 6。

到目前为止我的查询是

SELECT *
FROM table1
WHERE    
 (CustID = 20 AND Confirmed = 1 AND Cancelled <> 1 AND Completed <> 1) OR
 (CustID = 20 AND Confirmed = 1 AND Cancelled = 1 AND Completed <> 1) OR
 (CustID = 20 AND Confirmed = 1 AND Cancelled <> 1 AND Completed = 1)

任何帮助将不胜感激。

4

3 回答 3

21

您可以将其简化为:

SELECT * FROM table1
WHERE CustID = 20
  AND Confirmed = 1
  AND (Cancelled <> 1 OR Cancelled IS NULL)
  AND (Completed <> 1 OR Completed IS NULL)

在将实数值与 NULL 进行比较时,您必须小心(并且非常明确),因为等式和不等式都将它们排除在外。

于 2012-08-23T08:59:29.473 回答
3
SELECT *
FROM table1
WHERE    
    CustID = 20 AND Confirmed = 1 AND NOT (Cancelled = 1 OR Completed = 1)  
于 2012-08-23T08:59:40.990 回答
-2
SELECT * FROM thk_basvuru WHERE thk_status in (8,23,33) or thk_olo_status not in (4,6,9,11,12)
于 2014-02-04T08:59:36.767 回答