INNER JOIN由SQL中的UNION ALL创建的两个表

时间:2015-01-05 16:09:26

标签: sql

我想使用UNION ALL命令合并到表中。然后我想用另一个表INNER JOIN结果。我玩了以下结构,但它不起作用:

 (Select * from TableA
 UNION ALL
 Select * from TableB)
 INNER JOIN TableC
 ON somekey

2 个答案:

答案 0 :(得分:2)

您需要alias UNION ALL的结果并从中选择

SELECT  * 
FROM   (SELECT * FROM TableA UNION ALL SELECT * FROM TableB) ua 
       INNER JOIN TableC ON TableC.somekey = ua.SomeKey

答案 1 :(得分:2)

   select * 
   from   (Select * from TableA union all Select * from TableB) t1
   inner join TableC t2 on t1.somekey=t2.somekey;