加入两个表 - 一个有选择,一个未触及

时间:2017-11-28 22:02:40

标签: sql oracle

我正在尝试连接两个表并返回记录总数,但是第一个表必须具有WHERE条件才能首先筛选出数千条记录。第一个表格包含别名' Poly_NoRoad',第二个表格包含别名' Vri'。

我试图嵌套第一个表格选择(我根据各种不相似的标准筛选记录,最终得到一个简化的' Poly_NoRoad'表格)然后加入该选择的结果' Vri'表

OPENING_ID是两个表中的公共字段。

我在这里做错了什么?

Select count(*)
From
  (Select *
  From Whse_Forest_Vegetation.Rslt_Forest_Cover_Poly Poly_Noroad
  Where Upper(Poly_Noroad.Silv_Polygon_No) Not Like 'PAS%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%WTR%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%WTP%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%RD%'
  And Upper (Poly_Noroad.Silv_Polygon_No) Not Like 'RES%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%ROAD%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%LANDING%')

Join Whse_Forest_Vegetation.Veg_Comp_Lyr_R1_Poly Vri 
On Poly_Noroad.Opening_Id = Vri.Opening_Id

1 个答案:

答案 0 :(得分:1)

您需要为该派生表(“子选择”)提供别名,否则您无法加入该别名。派生表中的别名 在外部不可见。

Select count(*)
From (
  Select *
  From Whse_Forest_Vegetation.Rslt_Forest_Cover_Poly Poly_Noroad
  Where Upper(Poly_Noroad.Silv_Polygon_No) Not Like 'PAS%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%WTR%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%WTP%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%RD%'
  And Upper (Poly_Noroad.Silv_Polygon_No) Not Like 'RES%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%ROAD%'
  And Upper(Poly_Noroad.Silv_Polygon_No)  Not Like '%LANDING%'
) t --<< an alias for the derived table
  Join Whse_Forest_Vegetation.Veg_Comp_Lyr_R1_Poly Vri 
     On t.Opening_Id = Vri.Opening_Id;
        ^
        You need to use the alias of the derived table.