将 2 个 Oracle 查询合并为一个,其中包含一些用 null 指定的通用数据
merge 2 Oracle queries into one with some generic data specified with null
我可以编写一个可以执行类似操作的 Oracle 查询吗?
如果代码是 XXX,则 return V1,V2
如果 code 不是 XXX,则 return 那些 code = null 的值,即 V3、V4
表 1
code value
---- -----
XXX V1
XXX V2
null V3
null V4
我需要写这样的东西:
- select * 来自表 1,其中代码 = 'XXX'
- 如果没有行 return,运行 跟随查询
- select * 来自表 1,其中代码为 null
我不确定我是否完全符合您的要求。你可以用
之类的东西来实现你的要点
select *
from table1 t1
where t1.code = 'XXX'
union all
select *
from table1 t1_2
where t1_2.code is null
and not exists( select 1
from table1 t1_3
where t1_3.code = 'XXX' )
with q as
(select code, value from table1 where code = 'XXX')
select *
from q
union all
select code, value
from table1
where code is null and not exists (select 1 from q)
我使用 dbfiddle.uk 仅用于测试,但我认为这可行。只需删除“with table1 as”子句,因为它用于构建测试数据。
with table1 as
(
select 'XXX' as code, 'V1' as value from dual
union
select 'XXX' as code, 'V2' as value from dual
union
select null as code, 'V3' as value from dual
union
select null as code, 'V4' as value from dual
)
select *
from table1
where nvl(code, '!!!') = 'XXX'
union
select *
from table1
where code is null
and not exists(select 1 from table1 where code='XXX')
我可以编写一个可以执行类似操作的 Oracle 查询吗? 如果代码是 XXX,则 return V1,V2 如果 code 不是 XXX,则 return 那些 code = null 的值,即 V3、V4
表 1
code value
---- -----
XXX V1
XXX V2
null V3
null V4
我需要写这样的东西:
- select * 来自表 1,其中代码 = 'XXX'
- 如果没有行 return,运行 跟随查询
- select * 来自表 1,其中代码为 null
我不确定我是否完全符合您的要求。你可以用
之类的东西来实现你的要点select *
from table1 t1
where t1.code = 'XXX'
union all
select *
from table1 t1_2
where t1_2.code is null
and not exists( select 1
from table1 t1_3
where t1_3.code = 'XXX' )
with q as
(select code, value from table1 where code = 'XXX')
select *
from q
union all
select code, value
from table1
where code is null and not exists (select 1 from q)
我使用 dbfiddle.uk 仅用于测试,但我认为这可行。只需删除“with table1 as”子句,因为它用于构建测试数据。
with table1 as
(
select 'XXX' as code, 'V1' as value from dual
union
select 'XXX' as code, 'V2' as value from dual
union
select null as code, 'V3' as value from dual
union
select null as code, 'V4' as value from dual
)
select *
from table1
where nvl(code, '!!!') = 'XXX'
union
select *
from table1
where code is null
and not exists(select 1 from table1 where code='XXX')