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

INNER JOIN two tables which were created by UNION ALL in SQL

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

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

您需要 alias 您的 UNION ALL 和 select 的结果

SELECT  * 
FROM   (SELECT * FROM TableA UNION ALL SELECT * FROM TableB) ua 
       INNER JOIN TableC ON TableC.somekey = ua.SomeKey
   select * 
   from   (Select * from TableA union all Select * from TableB) t1
   inner join TableC t2 on t1.somekey=t2.somekey;