SQL Join 3表---数据未从内部表中提取

时间:2012-06-27 18:28:34

标签: sql oracle join

我有这个SQL查询从3个表中提取数据。我无法从table3获取任何列。可能是什么问题?

SELECT table1.col1        COUNTRY,
   table2_alias1.col3 STATE,
   table2.GEOG_LOCATION,
   table2.GEOG_LOCATION_pRE,
   table2.T2col4,
   table2.T2col5,
   TABLE3_ALIAS1.ZIP,
   TABLE3_ALIAS1.T3col2,
   TABLE3_ALIAS1.T3col3,
   TABLE3_ALIAS1.T3col4
FROM   table1,
   table2 table2_alias1,
   table2,
   (SELECT *
         FROM   table3
         WHERE  ( col5 = 'XXX'
                   OR col5 = 'YYY' )
                AND col6 = 'ZZZ')TABLE3_ALIAS1
      WHERE  table1.col3 = 'AAA'
              AND table1.col1 = table2_alias1.STATE
              AND table2_alias1.STATE = table2.col7(+)
              AND table2.STATE = TABLE3_ALIAS1.col6(+) 
ORDER BY 
2,
4,
10,
 9

样本输出(突出显示最重要的数据作为有意义的COL名称)

 COUNTRY|STATE| GEOG_LOCATION   | T2COL2   | T2col3 | t2col4 | zip | T3col2 |T3col4

 USA       CA     LosAngls       LosAngls-1   0        y      90001
 USA       CA     SanFrans       SanFrans-4   0        y      94101
 USA       CA     Freemont       Freemont-A   0        n      94538

只有前5列有数据,并且列INCLUDING zip(来自table3)及以后的列在运行查询时不包含任何数据

RDBMS

 TABLE1          TABLE2             TABLE3 

 T1_COL1         GEOG_LOCATION          T3_COL1 
 GEOG_LOCATION        T2_COL2       T3_COL2
 T1_COL3         GEOG_LOCATION_pRE      GEOG_LOCATION
 T1_COL4         T2_COL4        T3_COL4
 T1_COL5         T2_COL5        T3_COL5
 T1_COL6         T2_COL6        T3_COL6
 T1_COL7         T2_COL7        T3_COL7
 T1_COL8         T2_COL8        T3_COL8
 T1_COL9         T2_COL9        T3_COL9
 T1_COL10                   T3_COL10

这三个表由startihg列与HHH链接。他们共享相同的数据。以FFF开头的列(表2中的第3列)与table2的第1列(HHH_col1)具有递归关系。

1 个答案:

答案 0 :(得分:2)

尝试自己运行嵌套查询:

     SELECT *
     FROM   table3
     WHERE  ( col5 = 'XXX'
               OR col5 = 'YYY' )
            AND col6 = 'ZZZ'

你收到数据吗?

此外,为了安全起见,可能值得在上面的查询中使用表名为前缀添加前缀。

然后,尝试将其加入table2:

     SELECT *
     FROM   table3, table2
     WHERE  ( col5 = 'XXX'
               OR col5 = 'YYY' )
            AND col6 = 'ZZZ'
            AND table2.col3 = table3.col6(+)

...依此类推,一次一步地重建您的查询,直到数据丢失。然后你知道哪一步是罪魁祸首......