如何在子查询

时间:2015-08-10 06:57:49

标签: sql oracle subquery

我有以下查询 -

select * from 
Table1 t1 , table2 t2 ,
(select idCol from table3) t3

我的问题是 - 我可以在子查询中使用表t2吗?

像这样

select * from 
Table1 t1 , table2 t2 ,
(select idCol, t2.nameCol from table3) t3

显然这会导致错误invalid identifier t2.nameCol

但是,如果我写如下,它会给不必要的额外行

select * from 
Table1 t1 , table2 t2 ,
(select idCol,  t2.nameCol from table3, table2 t2) t3

其他任何方式吗?

修改

基本上我想要实现的是

select * from 
Table1 t1 , table2 t2 ,
(select 
    case
    when t2.nameCol = 'ABC'  then 'ABC'
    else idCol
    end idCol from table3) t3

1 个答案:

答案 0 :(得分:0)

仅在满足特定条件时才加入表称为外连接。方法如下:

select *
from table1 t1 
inner join table2 t2 on <join criteria here>
left outer join table3 t3 on t2.namecol <> 'ABC' and <join criteria here>;

但是,在您的情况下,将子查询移动到SELECT子句可能就足够了:

select 
  t1.*, 
  t2.*, 
  case when t2.namecol = 'ABC' then 
    'ABC' 
  else 
    (select idcol from table3 t3 where <join criteria here>) 
  end
from table1 t1 
inner join table2 t2 on <join criteria here>;