我需要为A和B列仅提取0个值的ID。一个例子:
+----+------+------+
| ID | A | B |
+----+------+------+
| 1 | null | 123 |
| 2 | 23 | 768 |
| 3 | 0 | 0 |
| 4 | 96 | 0 |
| 5 | 0 | null |
| 6 | 0 | 0 |
+----+------+------+
我尝试了几次查询,但是我仍在遍历大于0的值。由于表中存在空值,因此我使用NVL(expr1,0)语法将空值替换为0:
+----+------+------+
| ID | A | B |
+----+------+------+
| 1 | 0 | 123 |
| 2 | 23 | 768 |
| 3 | 0 | 0 |
| 4 | 96 | 0 |
| 5 | 0 | 0 |
| 6 | 0 | 0 |
+----+------+------+
我在WHERE子句中使用以下内容,并获得以下结果:
Where status = 'OPEN'
AND a.value IS NULL OR a.value = '0'
AND b.value IS NULL OR b.value = '0'
Output:
+----+----+-----+
| ID | A | B |
+----+----+-----+
| 1 | 0 | 123 |
| 3 | 0 | 0 |
| 5 | 0 | 0 |
| 6 | 0 | 0 |
+----+----+-----+
似乎我只为A拉取0个值,但对于B我仍然得到大于0的值。我只需要为A和B都拉取值为0的ID。
答案 0 :(得分:1)
我认为您只需要括号即可
Where status = 'OPEN' AND
(a.value IS NULL OR a.value = 0) AND
(b.value IS NULL OR b.value = 0)
答案 1 :(得分:0)
我喜欢戈登的回答,但为了简洁起见,我在这里使用COALESCE
:
SELECT *
...
WHERE
status = 'OPEN' AND
COALESCE(a.value, 0) = 0 AND
COALESCE(b.value, 0) = 0;
我们也可以用总和表示:
WHERE
status = 'OPEN' AND
COALESCE(a.value, 0) + COALESCE(b.value, 0) = 0;