Oracle查询:父/子

时间:2014-06-13 10:20:12

标签: sql oracle

我有2张桌子。 (table1& table2)

表1家长:

ID        | Description
11058744  | A
11058745  | A
11058746  | A

表2儿童:

ID        | REFCODE            
11058745  | 11058744
11058744  | 11058745
11058746  | 11058744
11058744  | 11058746

我希望结果如下所示:

ID        | REFCODE 
11058744  | 11058744 
11058745  | 11058744   
11058746  | 11058744  

1 个答案:

答案 0 :(得分:0)

以下查询将生成您请求的结果:

WITH A AS (SELECT DISTINCT ID AS ID FROM TABLE1),
     B AS (SELECT MIN(REFCODE) AS REFCODE FROM TABLE2)
SELECT A.ID, B.REFCODE
  FROM A
  CROSS JOIN B;

SQLFiddle here

分享并享受。