Oracle SQL从列返回特定值,并返回任何空值[已关闭]

hk8txs48  于 2023-04-29  发布在  Oracle
关注(0)|答案(2)|浏览(157)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

9小时前关闭
Improve this question
我有一个列中的值列表,其中一些行仍然为空。
我需要一个行的列表与1特定值,我想包括任何是空。
我可以得到特定的值,或者我可以得到空值。但我不能两者兼得。

SELECT source
FROM table
WHERE source = 'value1' OR source IS NULL

此选项需要很长的时间来处理,并且不包括任何空行
我也试过

WHERE source not in ('value2','value3','value4')

但是这个选项也排除了空值。
我需要我的查询返回的是

value1
value1
null
value1
null
null
value1
1cklez4t

1cklez4t1#

SELECT source
FROM table
WHERE coalesce(source,'value1') = 'value1'
uklbhaso

uklbhaso2#

也许像这样:

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

Where子句Nvl()函数将空值(如果有)视为您选择的值。..
您的第一次查询结果与上述示例数据相同。..

Select  VALS From list_tbl Where VALS = 'value1' Or VALS Is Null

相关问题