如何使用UNION&IN运算符创建灵活的搜索查询?

时间:2019-07-27 11:55:58

标签: hybris flexible-search

执行以下查询后,我从“ catalogA”和“ catalogB”两个目录中获得所有产品的联合 查询:

SELECT uniontable.PK, uniontable.creationtime AS TIMECREATED FROM
    (
       {{
          SELECT {p:mode},{p:brandno},{creationtime} AS creationtime FROM {Product AS p JOIN catalogversion AS cv ON
 {p:catalogversion}={cv:pk} JOIN catalog AS cat ON {p:catalog}={cat:pk}}
          WHERE {p.code} = '145' AND {cv:version}='Staged' AND {cat:id}='catalogA'
       }}
       UNION ALL
       {{
          SELECT {p:mode},{p:brandno},{creationtime} AS creationtime FROM {Product AS p JOIN catalogversion AS cv ON
 {p:catalogversion}={cv:pk} JOIN catalog AS cat ON {p:catalog}={cat:pk}}
          WHERE {p.code} = '123' AND {cv:version}='Staged' AND {cat:id}='catalogB'
       }}
    ) uniontable ORDERR BY TIMECREATED

我只需要UNION查询的CatalogA产品,所以我做了如下的子查询,但是抛出错误。任何帮助将不胜感激?

查询2:

SELECT uniontable.PK, uniontable.creationtime AS TIMECREATED FROM {Product AS p JOIN catalogversion AS cv ON
 {p:catalogversion}={cv:pk} JOIN catalog AS cat ON {p:catalog}={cat:pk}}
          WHERE {cv:version}='Staged' AND {cat:id}='catalogA' IN 
    (
       {{
          SELECT {p:mode},{p:brandno},{creationtime} AS creationtime FROM {Product AS p JOIN catalogversion AS cv ON
 {p:catalogversion}={cv:pk} JOIN catalog AS cat ON {p:catalog}={cat:pk}}
          WHERE {p.code} = '145' AND {cv:version}='Staged' AND {cat:id}='catalogA'
       }}
       UNION ALL
       {{
          SELECT {p:mode},{p:brandno},{creationtime} AS creationtime FROM {Product AS p JOIN catalogversion AS cv ON
 {p:catalogversion}={cv:pk} JOIN catalog AS cat ON {p:catalog}={cat:pk}}
          WHERE {p.code} = '123' AND {cv:version}='Staged' AND {cat:id}='catalogB'
       }}
    ) uniontable ORDERR BY TIMECREATED

1 个答案:

答案 0 :(得分:0)

您只需要使用WHERE uniontable.catalog = 'catalogA'

过滤临时表

喜欢

SELECT * FROM
(
   {{
      SELECT {p:pk},{p:mode},{p:brandno},{creationtime} AS creationtime, {cat:id} AS catalog 
      FROM {    Product AS p 
                JOIN catalogversion AS cv ON {p:catalogversion}={cv:pk} 
                JOIN catalog AS cat ON {p:catalog}={cat:pk}
            }
      WHERE {p.code} = '145' AND {cv:version}='Staged' AND {cat:id}='catalogA'
   }}
   UNION ALL
   {{
      SELECT {p:pk},{p:mode},{p:brandno},{creationtime} AS creationtime, {cat:id} AS catalog  
      FROM {    Product AS p 
                JOIN catalogversion AS cv ON {p:catalogversion}={cv:pk} 
                JOIN catalog AS cat ON {p:catalog}={cat:pk}
            }
      WHERE {p.code} = '123' AND {cv:version}='Staged' AND {cat:id}='catalogB'
   }}
) as uniontable WHERE uniontable.catalog = 'catalogA' ORDERR BY uniontable.creationtime