【问题标题】:Preforming set theory between a xmltype column and a subquery在 xmltype 列和子查询之间执行集合论
【发布时间】:2013-05-20 15:05:02
【问题描述】:

我正在使用旧应用程序上的 Oracle 数据库,但无法为其提出特定查询。

基本上我有一个 T_Selected 表,其中包含一个数字列(主键)和一个 xmltype 列。 XML 的格式为

<countries>
    <country>England</country>
    <country>Ireland</country>
    <country>Scotland</country>
    <country>Wales</country>
</countries>

我还有另一个表需要查询并使用结果

select country from T_Countries where language = 'English'

我需要三个查询及其否定。

  1. xml ∈ 子查询中的所有国家

  2. 子查询 ∈ xml 中的所有国家

  3. xml中的所有国家=所有子查询

我得到的最接近的是

select id from 
        T_Selected ts, 
        XMLTABLE('/countries/country'                  
                 passing ts.Values        
                 columns                                     
                 Country  path '//country'
                ) XML
        where XML.country in (select country from T_Countries 
                                                  where language ='English');

这将返回任何 xml 国家/地区在子查询中的 ID,而不是所有国家/地区。

有什么想法可以解决这个问题吗?

感谢您的帮助,

尼尔

【问题讨论】:

    标签: sql oracle xmltype set-theory


    【解决方案1】:
    1. SUB 的 XML 子集 选择编号 从 (选择身份证 从 T_选定的ts, XMLTABLE('/国家/地区' 传递 ts.Values 列 国家路径'//国家' ) XML) xml RIGHT OUTER join(从 T_Countries 中选择国家 其中语言 ='English') SUB) SUB.country=xml.country 上的 SUB 其中 XML.country 不为空;

    2. XML 的 SUB 子集 选择 xml.id 从 (从 T_选定的ts, XMLTABLE('/国家/地区' 传递 ts.Values 列 国家路径'//国家' ) XML) q1 LEFT OUTER join(从 T_Countries 中选择国家 where language ='English') SUB.country=xml.country 上的 SUB ) q2.country=q1.country 上的 q2 其中 q2.countyid 不为空;

    3. 联合 XML 和 SUB 选择id,国家来自 T_选定的ts, XMLTABLE('/国家/地区' 传递 ts.Values 列 国家路径'//国家' ) XML 联合所有 从 T_Countries 中选择 id、国家 其中语言 ='English') SUB.country=xml.country 上的 SUB ;

    【讨论】:

    • 您好,埃德,感谢您的回复。查询仍然不太正确,因为 select id from T_Selected ts, XMLTABLE('/countries/country' passing ts.Values columns Country path '//country' ) 为每个国家返回一行。这使得结果包含子查询中任何国家/地区的值,而不是所有国家/地区。
    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    相关资源
    最近更新 更多