简体   繁体   English

如何将三个不同表中的不同列合并到一张表中?

[英]How to combine different columns from three different tables into one table?

Please see the picture for reference.请参阅图片以供参考。 I tried doing Union All between the first and second select statements and also between the second and third select statements.我尝试在第一个和第二个 select 语句之间以及第二个和第三个 select 语句之间进行 Union All。 So I did two Union All but that did not work.所以我做了两个 Union All 但这没有用。 Can someone please help?有人可以帮忙吗?

When you use union , the number of columns and datatype of columns should be the same.使用union时,列数和列数据类型应相同。 So since your last query returns 'character', you have to convert the same column in other the queries into a string:因此,由于您的最后一个查询返回“字符”,因此您必须将其他查询中的同一列转换为字符串:

SELECT cast(MLDNBR as varchar(100)), cast(PLTGRN as varchar(100)), NULL as ReleaseID
FROM [DataWarehouse].[dbo].[v_Curing_Tooling] 
UNION ALL
SELECT cast(MOLDas varchar(100)), NULL, NULL
FROM [TireTrack].[dbo].[cos_work]
UNION ALL
SELECT Spec, Left (Spec,6) as SpecID, Right (Spec,2) as ReleaseID
FROM [DataWarehouse].[dbo].[Locator]

How is the data in v_Curing_Tooling , cos_work , and Locator related? v_Curing_Toolingcos_workLocator中的数据如何相关? If they are not related, they would not be on the same row.如果它们不相关,它们就不会在同一行。 If they are related, you need an ID to join the tables on.如果它们是相关的,您需要一个 ID 来加入这些表。

Example:例子:

SELECT MLDNBR, PLTGRN, MOLD, Spec, Left (Spec,6) as 'SpecID', Right (Spec,2) as 'ReleaseID'
FROM [DataWarehouse].[dbo].[v_Curing_Tooling] t
    JOIN [TireTrack].[dbo].[cos_work] w ON t.ToolingID = w.ToolingID
    JOIN [DataWarehouse].[dbo].[Locator] l ON l.LocationID = t.LocationID

I do not know how your tables are related, though, so those JOIN clauses probably need correct join conditions.不过,我不知道您的表是如何相关的,因此这些 JOIN 子句可能需要正确的连接条件。

Look up How To Join SQL Tables .查找如何加入 SQL 表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将来自不同表的三列合并为一行 - Combine three columns from different tables into one row 如何在一个表中合并来自不同表的单独列 - How to combine separate columns from different tables in a single table 如何将来自不同列的多个列组合到一个表或视图中? - How to combine multiple columns from different as columns in to one table or view? 将来自三个不同表的列合并为一个列 - Combine columns from three different tables into a single column 如何将三个不同表的输出存储到一个表中? - How to store the outputs from three different tables into one table? 如何从不同表中的不同列中检索数据并将其合并为一个报告 - How to retrieve data from different columns in different tables and combine them into one report Oracle SQL:是否可以将不同表中的列合并为一个表? - Oracle SQL: Can I combine columns from different tables into one table? 如何组合并从 MySQL 中的三个不同相关表中获取总和? - How to combine and get sum from three different related tables in MySQL? 如何将两个不同的表与不同的列组合 - How to combine two different tables with different columns 内部连接同一主表来自三个不同表的三个不同列,基于哪个不是 null - Inner join same master table three different columns from three different tables based on which is not null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM