【问题标题】:Recursive sql subset query, using connect by递归 sql 子集查询,使用 connect by
【发布时间】:2011-03-21 04:08:01
【问题描述】:

我有两张看起来有点像这样的表

AGR_TERR_DEF_TERRS
  ID
  DEF_ID
  TERRITORY_ID (foreign key links to TERRITORIES.ID)

TERRITORIES
  ID
  NAME
  PARENT_ID
(parent_id and id are recursive)

给定两个 DEF_ID,我需要一个函数来检查一个的领土是否是另一个的完整子集。 我一直在玩 CONNECT BY 和 INTERSECT,但写得一团糟而不是有用的函数。

我希望会有一个(相对)简单的 SQL 查询有效。

【问题讨论】:

    标签: sql oracle recursion set-theory


    【解决方案1】:

    考虑到获取给定 DEF_ID 的所有恐怖信息的查询(我不太确定您的表是什么),如果以下查询没有返回任何行,则 DEF_ID A 是 DEF_ID B 的子集:

    select statement for A
    MINUS
    select statement for B
    

    这有帮助吗?

    【讨论】:

    • 确实有帮助,谢谢。我现在还需要重新审视我的集合论:S
    【解决方案2】:

    基于@Tony Andrews 的回答,当 def_id_1 隐含的领土是 def_id_2 的子集时,这将产生零行,

    select id from territories start with id in
      (select territory_id from agr_terr_def_terrs where def_id = :def_id_1)
      connect by parent_id = prior id
    minus
    select id from territories start with id in
      (select territory_id from agr_terr_def_terrs where def_id = :def_id_2)
      connect by parent_id = prior id
    

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 2016-05-17
      • 2023-03-07
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多