目前使用SQLite3遇到问题
SELECT item_id, SUM(qoh), MIN(price), MAX(price), item_size
FROM inventory
WHERE item_size IS NULL OR item_size = 'S';
上述查询无效,只会返回NULL
个值,而不会返回S
。
SELECT item_id, SUM(qoh), MIN(price), MAX(price), item_size
FROM inventory
WHERE item_size = 'S'
以上显然会返回S
SELECT item_id, SUM(qoh), MIN(price), MAX(price), item_size
FROM inventory
WHERE item_size = 'S'OR item_size IS NULL;
以上只返回{{1}}值
我试图完成下面的等价物,
NULL
但在一个命令中,
由于SELECT item_id, SUM(qoh), MIN(price), MAX(price), item_size
FROM inventory
WHERE item_size IS NULL;
SELECT item_id, SUM(qoh), MIN(price), MAX(price), item_size
FROM inventory
WHERE item_size = 'S';
的性质,只返回WHERE column IS NULL OR column = 'value'
或'值'由于null影响算法的性质,
对我来说,SQL法律是否允许我绕过NULL
从根本上不起作用?
答案 0 :(得分:1)
使用
SELECT item_id, SUM(qoh), MIN(price), MAX(price), item_size
FROM inventory
WHERE coalesce(item_size, 'S') = 'S'
GROUP BY coalesce(item_size, 'S') -- will add nulls to S or
-- GROUP BY coalesce(item_size, 'null') - will add them as 'null' but filter them out by your where
您的问题是使用sum
隐式group by
- 并且null
与字符串不能很好地组合 - 您必须明确group by
合并后的值才能将空值分组到S