问题描述
我想弄清楚如何创建一个表来合并来自两个具有相同结构的表的记录。每个表将具有以下字段:作为主键的 document_ID、documentNumber、documentRevision 和 documentTitle。两个表的唯一区别是每个表都保存特定“类型”的文档。合并后的表最终应该包含两个表中的所有文档。
我曾尝试使用追加查询,但遇到了一个问题,即当记录插入到每个表中时,我无法弄清楚如何运行查询。 在网上搜索中,我看到了一些关于“加入”的内容,但我不知道如何使它起作用。
感谢任何帮助!
table1 : Documents of Type 1
+---------------+--------------------+-----------------+-------------------+
| Document ID | Document Number | Document Rev | Document Title |
+---------------+--------------------+-----------------+-------------------+
| 1 | GCD_111 | - | Title GCD1 |
| 2 | GCD_222 | A | Title GCD2 |
| 3 | GCD_333 | B | Title GCD3 |
+---------------+--------------------+-----------------+-------------------+
table2 : Documents of Type 2
+---------------+--------------------+-----------------+-------------------+
| Document ID | Document Number | Document Rev | Document Title |
+---------------+--------------------+-----------------+-------------------+
| 4 | TSR_111 | - | Title TSR1 |
| 5 | TSR_222 | A | Title TSR2 |
| 6 | TSR_333 | B | Title TSR3 |
+---------------+--------------------+-----------------+-------------------+
Result Table: Documents of Type 1 and Type 2
+---------------+--------------------+-----------------+-------------------+
| Document ID | Document Number | Document Rev | Document Title |
+---------------+--------------------+-----------------+-------------------+
| 1 | GCD_111 | - | Title GCD1 |
| 2 | GCD_222 | A | Title GCD2 |
| 3 | GCD_333 | B | Title GCD3 |
| 4 | TSR_111 | - | Title TSR1 |
| 5 | TSR_222 | A | Title TSR2 |
| 6 | TSR_333 | B | Title TSR3 |
+---------------+--------------------+-----------------+-------------------+
解决方法
您可以分解问题。首先,table1 和 table2 中哪些记录尚未在 resulttable 中?当您有一个查询来执行此操作时,将结果附加到结果表中。
我将对您的示例稍作修改,其中一条记录已在结果表中:
resulttable:
+-------------+-----------------+--------------+----------------+
| Document ID | Document Number | Document Rev | Document Title |
+-------------+-----------------+--------------+----------------+
| 1 | GCD_111 | - | Title GCD1 |
+-------------+-----------------+--------------+----------------+
一、查询
INSERT INTO resulttable
SELECT a.[Document ID],a.[Document Number],a.[Document Rev],a.[Document Title]
FROM
(SELECT t1.[document id],t1.[Document Number],t1.[Document Rev],t1.[Document Title]
FROM table1 AS t1
LEFT JOIN resulttable AS rt on t1.[Document ID] = rt.[Document ID]
WHERE rt.[Document ID] IS NULL
UNION ALL
SELECT t1.[document id],t1.[Document Title]
FROM table2 AS t1
LEFT JOIN resulttable as rt on t1.[Document ID] = rt.[Document ID]
WHERE rt.[Document ID] IS NULL) AS a
说明:table1 和table2 中的记录还没有出现在resulttable 中?
首先,对 table1 执行此操作。我们可以稍后对 table2 做同样的事情。
正如您提到的,我们可以使用 JOIN。我在这里使用了 LEFT JOIN 来计算出两个表 NOT 相交的位置,即查看所有 table1 记录并显示哪些记录还没有出现在结果表中。如果您选择 table1 中的所有记录和匹配的结果表记录(在 [Document ID] 上相交,您将更好地了解其工作原理:
select t1.*,rt.*
from table1 as t1
left join resulttable as rt on t1.[Document ID] = rt.[Document ID]
因为我们想要不匹配的部分,所以我们想要空位。因此,我们可以通过指定 WHERE 结果表记录为空并指定我们想要的列名来修改最后一个查询。这将使结果看起来正是我们希望他们寻找最终 INSERT 的方式。即:
select t1.[Document ID],t1.[Document Title]
from table1 as t1
left join resulttable as rt on t1.[Document ID] = rt.[Document ID]
WHERE rt.[Document ID] IS NULL
接下来,UNION ALL 也从表 2 中获得“新”结果。 (有关此 UNION ALL 的位置,请参阅最终查询。)
然后我将 SELECT.. UNION ALL SELECT.. 包裹在括号中,并给它一个别名“a”。这使我可以处理括号内的整个组,就好像它是一个名为“a”的表。
在那之后,剩下的就是一个普通的 INSERT 语句。