SQL查询,包含三个表的条件

时间:2013-01-28 14:33:57

标签: sql oracle

所以这里我有数据库的结构:

Tables

情况如下,假设我有一个ID为1294652442332的子元素“Sivu”以及更多属性(不重要)。 我需要选择页面(页面是Sivu的父元素),它们是我的Sivu的父母。当前工作的oracle DB查询是

SELECT
  DISTINCT parent.oid as parentid
                , parent.otype as parenttype
                , child.oid as childid
                , child.otype as childtype
                , child.ncode as ncode
                , child.nrank as nrank
FROM
  assetrelationtree child
        , assetrelationtree parent
        , Page parentasset
WHERE
  child.oid = 1294652442332 
  AND child.otype = 'Sivu'
  AND child.nparentid != 0
  AND child.ncode = '-'
  AND parent.nid = child.nparentid
  AND parent.otype = 'Page'
  AND parentasset.id = parent.oid
  AND parentasset.status != 'VO'
ORDER BY   
  nrank

现在,在图片中您将看到有一个站点规划表(OID字段包含页面ID)。它还有一个关于Page(Sivu的父元素)的重要事项。我想知道的重要领域是NCODE。 所以基本上,我想修改此查询以选择页面元素,其中表sitereeree中的NCODE字段具有特定值(NCODE ='Placed')。 我试图修改查询,但我卡住了,我甚至不知道是否可以通过一个选择:

SELECT
    DISTINCT parent.oid as parentid
                , parent.otype as parenttype
                , child.oid as childid
                , child.otype as childtype
                , child.ncode as ncode
                , child.nrank as nrank
FROM
    assetrelationtree child
        , assetrelationtree parent
       , Page parentasset
       , siteplantree siteplan
WHERE
    child.oid = 1294652442332
    AND child.otype = 'Sivu'
    AND child.nparentid != 0
    AND child.ncode = '-'
    AND parent.nid = child.nparentid
    AND parent.otype = 'Page'
    AND parentasset.id = parent.oid
    AND parentasset.status != 'VO'

  AND siteplan.ncode = 'Placed'
  AND siteplan.oid = child.nparentid
ORDER BY
    nrank

希望我已经描述了这个问题,以便你能理解。如果有些事情不清楚,请告诉我,我会更新说明。

1 个答案:

答案 0 :(得分:2)

AND siteplan.oid = child.nparentid更改为AND siteplan.oid = parentasset.id