从表中选择所有或仅特定的行

问题描述 投票:1回答:1

是否可以根据条件获取记录,如果条件不满足,则必须显示表中的所有记录。

例如,我有客户ID 1,2,3,4。如果我在where where条件中给出1作为c_id,它必须显示该特定记录。如果我将5作为c_id,它必须显示表中的所有记录。是否有可能在单个查询中实现?

以下是我试过的查询。

SELECT case
 WHEN c_id in ('6') then 1          
      else 0
       END as y from customer
oracle
1个回答
1
投票

你可以尝试这样的事情:

select *
from customer
where c_id = 6

union all

select *
from customer
where not exists (
    select null
    from customer
    where c_id = 6
)
© www.soinside.com 2019 - 2024. All rights reserved.