【问题标题】:How to/Possible to Generate String of Table Based Row Values for CONTAINS Search String?如何/可能为 CONTAINS 搜索字符串生成基于表的行值字符串?
【发布时间】:2013-07-24 08:45:58
【问题描述】:

所以理想情况下,我希望将其保留在查询中,但如果不可能,我想一个包可以工作,因为我没有 webapp 级别可以使用。

我想要做的是,对于 Oracle 数据库,在 where 子句中创建/运行一个查询,这样对于具有一个属性的表中的每一行,所有行都有带有通配符的子字符串存储它添加到contains 的搜索字符串。因为据我所知,您不能真正在查询中执行循环,因此需要一个游标,但我从未使用过游标。这是我正在尝试做的事情的更直观的表示(使用循环逻辑教唆):

表 1
属性:名字

约翰


约瑟芬

Table2
属性:子字符串

%se%
%h%i%

通过约束保证总是至少有一行

伪查询

SELECT 
  table1.firstname
FROM
  table1
WHERE CONTAINS(table1.firstname, '"table2.row1"
  IF(count(table2.substrings) > 1)
    FOR table2.row = 2 TO count(table2.substrings)
      (
      + " OR row.substrings"
      )
 ', 1) > 0

(CONTAINS 语法基于Is there a combination of "LIKE" and "IN" in SQL?)

【问题讨论】:

    标签: sql string oracle cursor contains


    【解决方案1】:

    我不太确定您想要得到什么,但我认为这些简单的示例可能会有所帮助。

    select * 
    from table1 t1
    where exists(
        select 1 from table2 t2
        where t1.firstname like t2.attribute
    );
    
    
    select t1.*,
           ( select listagg( ''''||t2.attribute||'''', ' OR ' ) WITHIN GROUP (order by t2.attribute )
             from table2 t2
             where t1.firstname like t2.attribute
           ) CONTAINS_argument
    from table1 t1
    

    这是这些查询的SQLFiddle demo

    【讨论】:

    • 第一个查询是我需要的,经过一番摆弄后,它可以在我的环境中工作。出于好奇,我尝试了第二个,因为我以前从未听说过 listagg 并且在第二行得到了 FROM not where 预期错误,但考虑到它是我需要的 #1,这并不是什么大不了的事。 DB 运行的是 Oracle 9,所以我怀疑这是一个遗留的语法问题。
    【解决方案2】:

    一些变体(根据我对问题的理解),基于collectionsmultiset casts 的用法:

    SQLFiddle

    所有匹配模式的字符串:

    select
      t1.firstname,
      cast( multiset(
        select t2.attribute
        from table2 t2 
        where t1.firstname like t2.attribute
      ) as sys.ODCIVarchar2List)              pattern_list
    from 
      table1 t1
    ;
    

    字符串匹配模式的所有模式:

    select 
      t2.attribute,
      cast( multiset(
        select t1.firstname
        from table1 t1 
        where t1.firstname like t2.attribute
      ) as sys.ODCIVarchar2List)               word_list
    from
      table2 t2
    ;
    

    即时构建模式集合:

    with table2 as (
      select '%se%' Attribute from dual union
      select '%h%i%' from dual   
    )
    select
      t1.firstname,
      cast( multiset(
        select t2.attribute
        from table2 t2 
        where t1.firstname like t2.attribute
      ) as sys.ODCIVarchar2List)               pattern_list
    from 
      table1 t1;
    

    过滤器只匹配:

    select 
      firstname, 
      (select count(1) from table(pattern_list)) cnt,
      pattern_list
    from (
      select
        t1.firstname,
        cast( multiset(
          select t2.attribute
          from table2 t2 
          where t1.firstname like t2.attribute
        ) as sys.ODCIVarchar2List)             pattern_list
      from 
        table1 t1
    )
    where (select count(1) from table(pattern_list)) > 0;
    

    等等。

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2017-04-21
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      相关资源
      最近更新 更多