【问题标题】:SQL Query with criteria on three tables在三个表上使用条件的 SQL 查询
【发布时间】:2013-01-28 14:33:56
【问题描述】:

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

情况如下,假设我有一个 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。 因此,基本上,我想修改此查询以选择在表 siteplantree 中具有 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

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

【问题讨论】:

  • 我猜这里缺少逗号:Page parentasset siteplantree siteplan 是一个错字(应为Page parentasset, siteplantree siteplan
  • 是的,我在提交问题时快速重写了查询并制作了该类型,但是,这不是问题。第二个查询返回空结果。
  • 尝试将AND siteplan.oid = child.nparentid 更改为AND siteplan.oid = parentasset.id
  • 看起来很有效,哈哈。如此简单,谢谢你,这拯救了我的一天!将进行调查并尝试了解它为什么不起作用。
  • @paul 。 . .您应该将您的评论放入一个答案中,然后可以对其进行投票和接受。

标签: sql oracle


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多