2013-05-20 21 views
0

我正在使用遺留應用程序上的一個oracle數據庫,並且無法爲其提供特定查詢。在xmltype列和子查詢之間執行set theory

基本上我有一個表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國家在子查詢中的ids,而不是所有這些國家。

關於如何去做這件事的任何想法?從 SUB 選擇ID 的

感謝您的幫助,

尼爾

回答

0
  1. XML子集(從 選擇ID T_Selected TS, XMLTABLE('/國家/國家 通過ts.Values 列 Country path'// country' )XML)xml RIGHT OUTER加入(從T_Countries選擇國家 where language ='English')SUB)SUB on SUB.country = xml.country 其中XML.country不爲空;從 XML 選擇xml.id 的

  2. SUB子集(選擇ID從 T_Selected TS, XMLTABLE('/國家/國家 傳遞ts.Values 列 鄉村路徑「//國 )XML)q1 LEFT OUTER join(從T_Countries選擇國家 where language ='English')SUB在SUB.country = xml.country)q2在q2.country = q1.country 其中q2.countyid不爲null;

  3. UNION XML和SUB 選擇ID,國家從 T_Selected TS, XMLTABLE('/國家/國家 通過TS。值 列 鄉村路徑「//國 )XML UNION ALL 選擇ID,國家從T_Countries 其中language = '英語')上SUB.country = xml.country SUB;

+0

嗨,Ed,謝謝你的迴應。查詢仍不完全正確,因爲從T_Selected ts中選擇標識,XMLTABLE('/ countries/country'傳遞ts.Values列國家路徑'// country')爲每個國家返回一行。這使得結果在任何一個國家處於子查詢中而不是所有國家中都具有價值。 – Niall