SQL - 带有 case 语句和通配符的 Where 子句

SQL - Where clause with case statement and wildcards

我在 Oracle 的 APEX 环境中工作,试图 return 一种情况下的所有值和另一种情况下的特定值。如果您不熟悉 APEX,它会使用低代码生成数据驱动的前端页面;本例使用列表中 select 的下拉菜单,我将该列表 selection 与从我的数据库中提取的值进行比较。

这将创建下拉列表(需要 2 列 - 显示列 [name] 和 return 列 [id]):

select distinct 'All users' as name,
 '99999' as id
from real_table
union
select distinct name,
 id
from real_table

该输入存储在一个变量中,我们称之为:LIST_INPUT。当 'All users' 被 select 编辑时,我想 select 来自 another_table 的所有值,并且只有当他们的 name/id 是 select 时与特定用户关联的那些值]编辑。下面是我必须尝试实现的代码,但没有骰子。

select name,
 id,
 other_col1,
 other_col2
from another_table
where case
  when :LIST_INPUT like '99999' then '%'
  else :LIST_INPUT
 end like id

当真实用户 ID 被 selected 时,这工作正常,但是当 'All users' 值被 selected 时,return什么都没有。我的逻辑是,我要求它将通配符与 ID 字段进行比较,因此它应该 return 一切,但它 return 什么都没有。

谢谢!

我认为您的 LIKE 运算符的操作数顺序错误。不应该吗?

select name,
 id,
 other_col1,
 other_col2
from another_table
where id like case
  when :LIST_INPUT like '99999' then '%'
  else :LIST_INPUT
  end

概率case语句不用,看:

select name,
       id,
       other_col1,
       other_col2
  from another_table
 where id = :LIST_INPUT
    OR :LIST_INPUT like '99999' -- You can use any other condition here