SQL查询将两列合并为单列 [英] SQL query to merge two columns into single column

查看:954
本文介绍了SQL查询将两列合并为单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择     tblClientDocument_Base.Document_name,tblJobDocument_Base.File_Name


FROM         tblClientDocument_Base JOIN

                       tblJobDocument_Base ON tblClientDocument_Base.Tenant_Id = tblJobDocument_Base.Tenant_Id

SELECT     tblClientDocument_Base.Document_name, tblJobDocument_Base.File_Name
FROM         tblClientDocument_Base JOIN
                      tblJobDocument_Base ON tblClientDocument_Base.Tenant_Id = tblJobDocument_Base.Tenant_Id

它返回

Document_name        File_name

Document_name        File_name

a                                    d

a                                   d

b                                    e

b                                   e

c                                     f

c                                    f

现在我想像这样合并

Document_name

Document_name

a

b

c

d

e

f

推荐答案

Hi Revati,

Hi Revati,

您将要使用带有UNION的SELECT INTO( link )到第三个表
那个将两个表的结果保存为一列。 

You are going to want to use the SELECT INTO with UNION (link) to a third table that holds the results from the two tables as a single column. 

-- Create tblClientDocument_Temp table.
 
SELECT tblClientDocument_Base.Document_name 
INTO tblClientDocument_Temp 
FROM tblClientDocument_Base
GO
 
-- Uses tblJobDocument_Base
 
SELECT tblJobDocument_Base.File_Name
INTO tblJobDocument_Temp
FROM tblClientDocument_Base   
UNION 
SELECT tblClientDocument_Base.Document_name 
FROM tblClientDocument_Temp; 
GO 
 
SELECT tblClientDocument_Base.Document_name  
FROM tblJobDocument_Temp;

这会创建两个表:

tblJobDocument_Temp

tblClientDocument_Temp

并在单列中执行UNION。以上示例尚未经过测试,仅作为示例。

And performs a UNION into a single column. The above example has not bee tested and serves as an example only.

如果您有其他问题,请告诉我们。

Please let us know if you have additional questions.


这篇关于SQL查询将两列合并为单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆