提问人:user21392923 提问时间:4/28/2023 最后编辑:Alex Pooleuser21392923 更新时间:10/17/2023 访问量:88
Oracle SQL 从列返回特定值,AND 任何空值 [closed]
Oracle SQL return specific value from column, AND any nulls [closed]
问:
我在一列中有一个值列表,其中某些行仍为 null。
我需要一个具有 1 个特定值的行列表,我想包括任何为 null 的行。
我可以得到特定的值,或者我可以得到空值。但我不能把两者放在一起。
SELECT source
FROM table
WHERE source = 'value1' OR source IS NULL
此选项需要很长时间才能处理,并且不包含任何空行
我也试过了
WHERE source not in ('value2','value3','value4')
但此选项也排除了 null。
我需要我的查询返回的是
value1
value1
null
value1
null
null
value1
答:
0赞
d r
4/29/2023
#1
也许是这样的:
WITH -- Sample data
list_tbl AS
( Select 'value1' "VALS" From Dual Union All
Select 'value9' "VALS" From Dual Union All
Select 'value7' "VALS" From Dual Union All
Select 'value1' "VALS" From Dual Union All
Select NULL "VALS" From Dual Union All
Select 'value1' "VALS" From Dual Union All
Select 'valueX' "VALS" From Dual Union All
Select NULL "VALS" From Dual Union All
Select NULL "VALS" From Dual Union All
Select 'value2' "VALS" From Dual Union All
Select 'value3' "VALS" From Dual Union All
Select 'value1' "VALS" From Dual
)
--
-- SQL
Select VALS From list_tbl Where Nvl(VALS, 'value1') = 'value1'
--
-- Result
VALS
------
value1
value1
null
value1
null
null
value1
其中,子句 Nvl() 函数将空值(如果有的话)视为您选择的值......
您的第一个查询结果与上述示例数据相同...
Select VALS From list_tbl Where VALS = 'value1' Or VALS Is Null
0赞
Brian Leach
4/29/2023
#2
SELECT source
FROM table
WHERE coalesce(source,'value1') = 'value1'
评论
0赞
user21392923
4/29/2023
通过添加 OR 语句,我能够获得我所追求的查询返回。谢谢大家的建议。
评论
and does not include any null rows
...您能通过一些示例数据向我们展示您所说的“空行”是什么意思吗?dump()