[英]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_Tooling
、 cos_work
和Locator
中的数据如何相关? 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.