选择结果并在另一个选择中使用

时间:2013-01-24 13:32:44

标签: sql select nested

我想知道是否有办法做这样的事情。

select outsidecolumn, example1, example2
from (
    select exemple1,example2 
    from tablenames 
    inner joins
    where conditions) as p,
    table2 tb
    where p.example1 = tb.outsidecolumn

2 个答案:

答案 0 :(得分:0)

我认为这就是你想要的:

select tb.outsidecolumn, p.example1, p.example2
from 
(
  select exemple1,example2 
  from tablenames 
  inner joins
  where conditions
) p
inner join table2 tb
  on p.example1 = tb.outsidecolumn

答案 1 :(得分:0)

替代方案 - CTE

inner join也代替笛卡尔,尽管执行计划可能会相同,但可以说更具可读性。

with p as (
    select exemple1,example2 
    from tablenames 
    inner joins
    where conditions
)
select tb.outsidecolumn, p.example1, p.example2
from p
    join table2 tb
    on tb.outsidecolumn = p.example1