where 子句有问题
Something wrong into where clause
我有以下select:
select sum(col1) as sum1
from table1
where col2 = 'A' and col3 in ('AA','BB')
现在我做了:
select sum(col1) as sum2
from table1
where col2 <> 'A' and col3 not in ('AA','BB')
并尝试添加:sum1+sum2,我应该得到 (col1) 的总和。但是缺少一些价值。你知道为什么吗?
这两个查询并不完全互补,您缺少 NULL
个值来启动。您还缺少 WHERE col2 = 'A' AND col3 NOT IN ('AA', 'BB')
和 WHERE col2 <> 'A' and col3 IN ('AA', 'BB')
.
就像我说的你也缺少NULL
,你可以这样检查:WHERE col2 IS NULL OR col3 IS NULL
编辑请求的是第一个给定的备用查询:
select sum(col1) as sum2
from table1
where col2 <> 'A'
OR col3 not in ('AA','BB')
OR col2 IS NULL
OR col3 IS NULL
因为可能有行 col2 = 'A' 而 col3 不在 ('AA','BB')
要么
('AA','BB')
中的 col2 <> 'A' 和 col3
我有以下select:
select sum(col1) as sum1
from table1
where col2 = 'A' and col3 in ('AA','BB')
现在我做了:
select sum(col1) as sum2
from table1
where col2 <> 'A' and col3 not in ('AA','BB')
并尝试添加:sum1+sum2,我应该得到 (col1) 的总和。但是缺少一些价值。你知道为什么吗?
这两个查询并不完全互补,您缺少 NULL
个值来启动。您还缺少 WHERE col2 = 'A' AND col3 NOT IN ('AA', 'BB')
和 WHERE col2 <> 'A' and col3 IN ('AA', 'BB')
.
就像我说的你也缺少NULL
,你可以这样检查:WHERE col2 IS NULL OR col3 IS NULL
编辑请求的是第一个给定的备用查询:
select sum(col1) as sum2
from table1
where col2 <> 'A'
OR col3 not in ('AA','BB')
OR col2 IS NULL
OR col3 IS NULL
因为可能有行 col2 = 'A' 而 col3 不在 ('AA','BB') 要么 ('AA','BB')
中的 col2 <> 'A' 和 col3