简体   繁体   English

无论存在什么,如何编写查询返回值?

[英]How to write query to return value regardless of existance?

Given this: 鉴于这种:

   with data_row as  (select 1 as col_1 from dual)
   select 'Y' as row_exists from dual where exists 
   (select null 
       from data_row
      where col_1 in (2,1))

How can I get this? 我怎么能得到这个?

Col_1  Row_exists
--------------------
1       Y
2       N

In order to get a row of output, you need a row of input. 要获得一行输出,您需要一行输入。 You want to get the second row with a "2", but there is no table with that value. 您希望第二行显示“2”,但没有包含该值的表。

The approach is to generate a table that has the values that you want, and then use left outer join to find which match: 方法是生成一个具有所需值的表,然后使用left outer join来查找匹配项:

with data_row as (
     select 1 as col_1
     from dual
    ),
    what_i_care_about as (
     select 1 as col from dual union all
     select 2 from dual
    )
select wica.col,
      (case when dr.col_1 is NULL then 'N' else 'Y' end) as row_exists
from what_i_care_about wica left outer join
     data_row dr
     on wica.col = dr.col_1;

You cannot do directly what you want -- which is to create a row for each missing value in the in list. 这是创建一个列在每个缺失值-你想要什么,你不能直接做in列表中。 If you have a lot of values and they are consecutive numeric, then you can use connect by or a recursive CTE to generate the values. 如果您有很多值并且它们是连续数字,那么您可以使用connect by或递归CTE来生成值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM