SQL 如何将单个数据库调用中两个表的数据合并到相同的列中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/649066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:26:19  来源:igfitidea点击:

How do I merge data from two tables in a single database call into the same columns?

sqlmerge

提问by Middletone

If I run the two statements in batch will they return one table to two to my sqlcommand object with the data merged. What I am trying to do is optimize a search by searching twice, the first time on one set of data and then a second on another. They have the same fields and I'd like to have all the records from both tables show and be added to each other. I need this so that I can sort the data between both sets of data but short of writing a stored procedure I can't think of a way of doing this.

如果我批量运行这两个语句,它们会将一个表返回到两个表,并将数据合并到我的 sqlcommand 对象中。我想要做的是通过搜索两次来优化搜索,第一次搜索一组数据,然后搜索另一组数据。它们具有相同的字段,我希望两个表中的所有记录都显示并相互添加。我需要这个,以便我可以对两组数据之间的数据进行排序,但由于没有编写存储过程,我想不出这样做的方法。

Eg. Table 1 has columns A and B, Table 2 has these same columns but different data source. I then wan to merge them so that if a only exists in one column it is added to the result set and if both exist it eh tables the column B will be summed between the two.

例如。表 1 具有 A 列和 B 列,表 2 具有这些相同的列但数据源不同。然后我想合并它们,以便如果 a 仅存在于一列中,则将其添加到结果集中,如果两者都存在,则 eh 表中的列 B 将在两者之间求和。

Please note that this is not the same as a full outer join operation as that does not merge the data.

请注意,这与不合并数据的完全外连接操作不同。

[EDIT]

[编辑]

Here's what the code looks like:

代码如下所示:

Select * From
 (Select ID,COUNT(*) AS Count From [Table1]) as T1
     full outer join
 (Select ID,COUNT(*) AS Count From [Table2]) as T2
     on t1.ID = T2.ID

回答by Tordek

Perhaps you're looking for UNION?

也许您正在寻找 UNION?

IE:

IE:

SELECT A, B FROM Table1
UNION
SELECT A, B FROM Table2

回答by John Fouhy

Possibly:

可能:

select table1.a, table1.b
from table1
where table1.a not in (select a from table2)
union all
select table1.a, table1.b+table2.b as b
from table1
inner join table2 on table1.a = table2.a

edit: perhaps you would benefit from unioning the tables beforecounting. e.g.

编辑:也许您会计数之前从联合表格中受益。例如

select id, count() as count from
(select id from table1
 union all
 select id from table2)

回答by Chris

To do it, you would go:

要做到这一点,你会去:

SELECT * INTO TABLE3 FROM TABLE1
UNION
SELECT * FROM TABLE2

Provided both tables have the same columns

如果两个表具有相同的列

回答by RedFilter

I think what you are looking for is this, but I am not sure I am understanding your language correctly.

我认为您正在寻找的是这个,但我不确定我是否正确理解您的语言。

select id, sum(count) as count
from (
    select id, count() as count
    from table1
    union all
    select id, count() as count
    from table2
) a
group by id

回答by Darrel Miller

I'm not sure if I understand completely but you seem to be asking about a UNION

我不确定我是否完全理解,但您似乎在询问 UNION

SELECT A,B
FROM tableX
UNION ALL
SELECT A,B
FROM tableY