繁体   English   中英

oracle sql中where条件的动态数

[英]dynamic number of where condition in oracle sql

我需要在报告工具中为提示编写一个sql。 我得到一个由' - '分隔的变量中的多个值的列表,这些值的数量可以变化。(例如,“abc-def”eg2。“abc-def-xyz”)。

现在我需要在这个表单的oracle中编写sql(逻辑上)

select something
  from somewhere
 where someColumn in 'abc' -- use substr to extract abc
    or someColumn in 'def' -- use substr to extract def
    or ...till we don't find '-'.

我不能使用plsql进行查询。 我可能不知道在plsql中使用我选择的变量,或者报告工具可能不支持。

提前致谢。

尝试

select something
  from somewhere
 where someColumn in (select regexp_substr('abc-def-xyz','[^-]+', 1, level) from dual
                     connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null);

概括(考虑你的字段用“ - ”分隔)

select something
  from somewhere
 where someColumn in (select regexp_substr(variable,'[^-]+', 1, level) from dual
                     connect by regexp_substr(variable, '[^-]+', 1, level) is not null);

基本上子查询的输出如下所示 -

  SQL> select regexp_substr('abc-def-xyz','[^-]+', 1, level) value from dual
      connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null;

VALUE                            
-------------------------------- 
abc                              
def                              
xyz  

首先使用Oracle的regexp_substr()函数将字符串拆分为其部分。 请参阅regexp_substr函数 (如果您可以访问生成字符串的原始文件,请使用该函数 。)

其次,将它放在临时表中,然后只需:

select something
  from somewhere
 where someColumn in (select parts from tmpTable)
select something
  from somewhere
 where INSTR('-'||'abc-def-ghi'||'-', '-'||someColumn||'-') > 0;

如果你只想要一个包含' - '的结果列表,你可以做类似的事情

从SOMECOLUMN喜欢的某个地方选择某些东西('% - %');

暂无
暂无

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

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