如何将表列数据合并到另一个表列数据SQL中

时间:2022-09-05 16:41:41

I have 2 SQL tables each with a column that represents the name... Name, FName.

我有2个SQL表,每个表都有一个代表名称的列... Name,FName。

I would like to programmatically select all of the Names in Table1 and all of the names in Table2 and view them as if they were all in one column called Name.

我想以编程方式选择Table1中的所有名称和Table2中的所有名称,并将它们视为名为Name的一个列中的所有名称。

I have this so far, its not what I expected.

到目前为止,我有这个,这不是我的预期。

SELECT
    t1.FName AND t2.Name as Name
FROM 
    Table1 t1, Table2 t2

so imagine this

想象一下

如何将表列数据合并到另一个表列数据SQL中

2 个解决方案

#1


1  

Based on the image you added to the question, it seems you want a single column of unique names from those 2 tables with no concatenation involved. You can get that with a UNION query.

根据您添加到问题中的图像,您似乎需要来自这两个表的单列唯一名称,而不涉及连接。您可以使用UNION查询来获取它。

SELECT
    t1.Name AS [Name]
FROM 
    Table1 AS t1
UNION
SELECT
    t2.FName as [Name]
FROM 
    Table2 AS t2

#2


2  

In MySQL and SQL Server:

在MySQL和SQL Server中:

SELECT CONCAT(t1.FName, ' ', t2.Name) AS FullName
FROM Table1 t1, Table2 t2

In MS Access you would do the following:

在MS Access中,您将执行以下操作:

SELECT t1.FName & ' ' & t2.Name AS FullName
FROM Table1 t1, Table2 t2

If you want all rows contained in on field then you really need the GROUP_CONCAT function. However, there is no GROUP_CONCAT in Microsoft Access. You will probably have to use some VBA to accomplish this task. Take a look at: Concatenate records and GROUP BY in Access.

如果你想要在on字段中包含所有行,那么你真的需要GROUP_CONCAT函数。但是,Microsoft Access中没有GROUP_CONCAT。您可能必须使用一些VBA来完成此任务。看看:在Access中连接记录和GROUP BY。

EDIT:

Now your update is asking something totally different. If you want to the above result the following will give that to you without any duplicates:

现在你的更新正在问一些完全不同的东西如果您想要上述结果,以下内容将为您提供没有任何重复的内容:

SELECT t1.FName AS [Name]
FROM Table1 AS t1
UNION
SELECT t2.Name as [Name]
FROM Table2 AS t2

However, if t1 and t2 has a record that is the same and you don't want them to be combined then you would want to use:

但是,如果t1和t2的记录相同并且您不希望它们合并,那么您可能希望使用:

SELECT t1.FName AS [Name]
FROM Table1 AS t1
UNION ALL
SELECT t2.Name as [Name]
FROM Table2 AS t2

#1


1  

Based on the image you added to the question, it seems you want a single column of unique names from those 2 tables with no concatenation involved. You can get that with a UNION query.

根据您添加到问题中的图像,您似乎需要来自这两个表的单列唯一名称,而不涉及连接。您可以使用UNION查询来获取它。

SELECT
    t1.Name AS [Name]
FROM 
    Table1 AS t1
UNION
SELECT
    t2.FName as [Name]
FROM 
    Table2 AS t2

#2


2  

In MySQL and SQL Server:

在MySQL和SQL Server中:

SELECT CONCAT(t1.FName, ' ', t2.Name) AS FullName
FROM Table1 t1, Table2 t2

In MS Access you would do the following:

在MS Access中,您将执行以下操作:

SELECT t1.FName & ' ' & t2.Name AS FullName
FROM Table1 t1, Table2 t2

If you want all rows contained in on field then you really need the GROUP_CONCAT function. However, there is no GROUP_CONCAT in Microsoft Access. You will probably have to use some VBA to accomplish this task. Take a look at: Concatenate records and GROUP BY in Access.

如果你想要在on字段中包含所有行,那么你真的需要GROUP_CONCAT函数。但是,Microsoft Access中没有GROUP_CONCAT。您可能必须使用一些VBA来完成此任务。看看:在Access中连接记录和GROUP BY。

EDIT:

Now your update is asking something totally different. If you want to the above result the following will give that to you without any duplicates:

现在你的更新正在问一些完全不同的东西如果您想要上述结果,以下内容将为您提供没有任何重复的内容:

SELECT t1.FName AS [Name]
FROM Table1 AS t1
UNION
SELECT t2.Name as [Name]
FROM Table2 AS t2

However, if t1 and t2 has a record that is the same and you don't want them to be combined then you would want to use:

但是,如果t1和t2的记录相同并且您不希望它们合并,那么您可能希望使用:

SELECT t1.FName AS [Name]
FROM Table1 AS t1
UNION ALL
SELECT t2.Name as [Name]
FROM Table2 AS t2