组合 2 个不同但相当相似的表

发布于 2024-08-05 20:00:05 字数 870 浏览 2 评论 0原文

我有两个相似但不相同的表,因此不可能联合。我需要合并这些表,记住大约有 40 列,其中只有 20 列是两者共有的。关于最佳方法有什么想法吗?

Table1

ActivityCategory    ActivityType   Nationality   Language
---------------------------------------------------------
Communication       Telephone      French        French
Meeting             Session        British       English


Table2

ActivityCategory    ActivityType   Nationality   Employment
-----------------------------------------------------------
Communication       Fax            American      Employed


Combined Table

ActivityCategory    ActivityType   Nationality   Language   Employment
----------------------------------------------------------------------
Communication       Telephone      French        French
Meeting             Session        British       English
Communication       Fax            American                 Employed

I have 2 tables that are similar but not the same so a union is not a possibility. I need to combine the tables bearing in mind there's about 40 columns where only 20 are common to both. Any ideas on the best approach?

Table1

ActivityCategory    ActivityType   Nationality   Language
---------------------------------------------------------
Communication       Telephone      French        French
Meeting             Session        British       English


Table2

ActivityCategory    ActivityType   Nationality   Employment
-----------------------------------------------------------
Communication       Fax            American      Employed


Combined Table

ActivityCategory    ActivityType   Nationality   Language   Employment
----------------------------------------------------------------------
Communication       Telephone      French        French
Meeting             Session        British       English
Communication       Fax            American                 Employed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜尕妞 2024-08-12 20:00:05

进行 UNION,但首先必须使表的结构相同。

在 Table1 的 SELECT 中,添加“Employment”作为 NULL 值列

在 Table2 的 SELECT 中,添加“Language”作为 NULL 值列

SELECT ActivityCategory, ActivityType, Nationality, Language, NULL AS Employment
FROM Table1
UNION
SELECT ActivityCategory, ActivityType, Nationality, NULL AS Language, Employment
FROM Table1

Do a UNION but first you have to make the tables same in structure.

In the SELECT for Table1, add Employment as a NULL value column

In the SELECT for Table2, add Language as a NULL value column

SELECT ActivityCategory, ActivityType, Nationality, Language, NULL AS Employment
FROM Table1
UNION
SELECT ActivityCategory, ActivityType, Nationality, NULL AS Language, Employment
FROM Table1
沉默的熊 2024-08-12 20:00:05

联合什么可以联合,然后加入其余的列?

如果您只需要执行一次,则很有可能。我想即使有一个观点也是可能做到的。

Union what is possible to union, and later on join the rest of the collumns?

Pretty possible if you will need to do it only one time. I guess that even with a view its possible to do it.

南街九尾狐 2024-08-12 20:00:05

Raj 的建议很好,但您可能想要执行 UNION ALL 而不是 UNION。

来自 BOL

UNION ALL 将所有行合并到结果中。这包括重复项。如果未指定,重复的行将被删除。

使用 UNION 需要付出一定的代价来保证没有重复项,但就您而言,听起来无论如何您都不会出现重复项。

Raj's suggestion is good, but you will probably want to do a UNION ALL and not a UNION.

From BOL,

UNION ALL incorporates all rows into the results. This includes duplicates. If not specified, duplicate rows are removed.

Using UNION pays the price to guarantee no duplicates, but in your case, it sounds like you won't have duplicates anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文