0

我有一个查询

SELECT sum(cash)  from bought_cash WHERE uid=1 AND source NOT IN ('a', 'b')  

结果为140

 SELECT sum(cash)  from bought_cash WHERE uid=1 AND source  IN ('a', 'b')  

NULL

SELECT sum(cash)  from bought_cash WHERE uid=1 

结果为240

SELECT sum(cash)  from bought_cash WHERE uid=1 and source is null  

结果为100

如何编写查询,以便第一个查询240通过包含空值来给出结果。

4

2 回答 2

1

您也可以尝试下一个:

select sum(cash) 
from bought_cash
where uid = 1 and (source is null or source not in ('a', 'b'))
于 2013-02-04T19:09:48.937 回答
0

你可以尝试这样的事情:

select sum(cash) from bought_cash 
    where uid=1 and isnull(source, 'c') not in ('a', 'b')

这应该将空条目映射到值“c”,它不是“a”或“b”,因此应该包含在结果集中。

于 2013-02-04T19:06:33.883 回答