将多个表连接到一个表

时间:2014-03-10 15:30:22

标签: sql-server

我有表A,B,C,我想将这些表连接成一个表但是表A没有表B和表C的链接字段。表B和表C有。

1 个答案:

答案 0 :(得分:0)

使用UNION ALL,您必须将列命名为相同:

SELECT COLA1 AS [COMMON_COL1], COLA2 AS [COMMON_COL2]
FROM A
UNION ALL
SELECT COLB1 AS [COMMON_COL1], COLB2 AS [COMMON_COL2]
FROM B
UNION ALL
SELECT COLC1 AS [COMMON_COL1], COLC2 AS [COMMON_COL2]
FROM C
ORDER BY [COMMON_COL1]