首页 文章

如果没有找到带有IN子句的oracle查询的行,则返回null

提问于
浏览
0

我有一个有三列的表 . 我用IN子句查询该表 .

从table1中选择column1,其中(1,2,3)中的column1按column2,column3排序

table1仅包含column1中的值1和2 . 我想在结果中返回不可用的值,并且应该在底部排序 .

示例数据

column1第2列第3列

1   100 11

2   101 50

输出,不可用的值应该在最后 .

column1第2列第3列

1   100 11

2   101 50

3 null null

我尝试使用NVL的子查询,就像选择nvl((select ... in(1,2,3)),null)来自dual,由于IN子句,我得到的单行子查询返回多行问题,这是预期 .

也试过工会,但没有任何作用 . 很好,如果有任何帮助谢谢

2 回答

  • 0

    如果您不能从另一个表中获取1,2,3值,您可以尝试:

    with t1 as (
      select col1,col2,col3
      from tab1
      where cod_flusso in ('1','2','3')),
    t2 as (
      select '1' as col1,null,null
      from dual
      union
      select '2',null,null
      from dual
      union
      select '3',null,null
      from dual)
    select t2.col1,col2,col3
    from t2
    left outer join t1
    on t1.col1= t2.col1
    

    如果你可以在第二个表中存储1,2,3值,那么最好使用左外连接 .

  • 0

    我认为你可以用一个联盟来做到这一点:

    select column1 from table1 where column1 in (1,2,3) order by column2, column3 
    union all 
    select null from table1 where column1 not in (1,2,3) order by column2, column3
    

相关问题