是否可以加入嵌套查询?

时间:2013-09-25 12:48:12

标签: sql

我正在尝试两个嵌套查询。

select * from 
   select a.*, b.* from( 
   (select AAA, BBB from Atable) a) as AA,
   select * from (
   (select QQQ, BBB from Btable)b) as BB
where AA.AAA = 'Y' and AA.BBB = BB.BBB

可以做这样的事吗?

2 个答案:

答案 0 :(得分:2)

你可以这样做:

select * from 
   (select AAA, BBB from Atable) a
      inner join
   (select QQQ, BBB from Btable)b
      on
         a.BBB = b.BBB
where a.AAA = 'Y'

我不确定你问题中的嵌套和别名的额外级别是什么。

答案 1 :(得分:0)

为什么要转而查询此类查询

select * 
from 
   select a.*, b.* 
   from
       ( 
           (select AAA, BBB from Atable) a) as AA,
           select * 
           from (
                  (select QQQ, BBB from Btable) b) as BB
where 
    AA.AAA = 'Y' and AA.BBB = BB.BBB

您可以使用任何连接,如内连接,左外连接等,如此

select 
    AA.AAA, AA.BBB, BB.QQQ 
from 
    Atable as AA
inner join
    Btable as BB ON BB.BBB = AA.BBB
where
    AA.AAA = 'Y'