简体   繁体   English

合并数据表列

[英]Merge Datatable columns

Hi I have two data tables say: 嗨,我有两个数据表说:

Table 1: 表格1:

name  age
----  ---
Bob   40

Table 2: 表2:

auto kids
---- ----
3     3

I want to merge the two tables to get something like 我想合并两个表以获得类似

name age auto kids
---  --- --- ----
Bob  40   3   3

Is this possible? 这可能吗? I tried merge but it does not seem to be working, thanks. 我尝试了合并,但是似乎无法正常工作,谢谢。


The reason I cannot do what you guys are suggesting is because the two data comes from two different databases(Oracle, SQL). 我无法执行您所建议的原因是因为这两个数据来自两个不同的数据库(Oracle,SQL)。 I cannot use linkedservers because the speed is just not there, is there something you can suggest to me that I can do programatically? 我无法使用链接服务器,因为速度还不存在,您是否可以建议我以编程方式进行操作? Thanks. 谢谢。

I'm going to take a shot at adding to the previous two answers in the hopes of making it clearer for you in case it's not. 我将尝试在前面的两个答案中添加一些内容,以期为您提供更清晰的信息。 What Stingy and Menace are wondering why the two tables you speak of don't look something like this: Stingy和Menace想知道为什么您所说的两个表看起来不是这样:

Person
PersonID (primary key) PersonID(主键)
FirstName 名字
LastName
Age 年龄

Demographics 客层
DemographicsID (primary key) 人口统计ID(主键)
PersonID (foreign key) PersonID(外键)
Autos 汽车
Kids 孩子们

... if the two tables looked like this then you could combine them by adding a few columns to the person table so that it looked like this: ...如果两个表看起来像这样,那么您可以通过在人员表中添加几列来合并它们,使其看起来像这样:

Person
PersonID (primary key) PersonID(主键)
FirstName 名字
LastName
Age 年龄
Autos 汽车
Kids 孩子们

... then executing a query like this: ...然后执行如下查询:

UPDATE Person p, Demographics d 更新人员p,受众特征d
SET
p.Autos = d.Autos p。汽车= d。汽车
p.Kids = d.Kids p。孩子= d。孩子
WHERE 哪里
p.PersonID = d.PersonID p.PersonID = d.PersonID

In the example above, without the PersonID field in both the Person and Demographics tables we don't know which Demographic record is associated with each Person record. 在上面的示例中,在“个人”和“人口统计”表中都没有“ PersonID”字段时,我们不知道哪个人口统计记录与每个“个人”记录相关联。 You need to know that in order to create the combined table. 您需要知道这一点才能创建组合表。

Merge will only combine the data of tables with similar schema. 合并将仅合并具有类似架构的表的数据。

You will probably have to iterate through each table and make a 3rd table that has all the columns you want. 您可能必须遍历每个表并制作一个包含所有所需列的第3个表。

Also, I dont see any kind of relationship key here. 另外,我在这里看不到任何关系键。 If you had that, you could make a data relation and not need to even merge them. 如果有的话,您可以建立数据关系,甚至不需要合并它们。

how do these tables relate? 这些表如何关联? What is the key between them 他们之间的关键是什么

select * from table1 t1
join table2 t2 on  <some magic where clause here>

If you want to join this two data tables - Table1(name,age) and Table2(auto,kids) regardless any joining between columns then i would prefer to use Linq. 如果要联接这两个数据表-Table1(name,age)和Table2(auto,kids),无论列之间是否有联接,那么我都希望使用Linq。 In Linq you can join two or more tables with the "rowIndex" of the tabel. 在Linq中,您可以使用表的“ rowIndex”联接两个或多个表。

Try following VB.NET code: 尝试使用以下VB.NET代码:

Dim Table1, Table2, MergerTable As New DataTable

    Dim rs = From c In Table1.AsEnumerable()
        Join c1 In Table2.AsEnumerable() On Table1.Rows.IndexOf(c) Equals LicensDatum.Rows.IndexOf(c1)
                   Select New With
                   {
                        .a0 = c.Item(0),
                        .b0 = c.Item(0),
                        .a1 = c.Item(0),
                        .b1 = c1.Item(0)
                   }

    MergerTable = New DataTable()
    MergerTable.Columns.Add("name", GetType(String))
    MergerTable.Columns.Add("age", GetType(String))
    MergerTable.Columns.Add("auto", GetType(String))
    MergerTable.Columns.Add("kid", GetType(String))

    For Each row In rs.ToList()
        Dim tableRow = MergerTable.NewRow()
        tableRow.Item("name") = row.a0
        tableRow.Item("age") = row.b0
        tableRow.Item("auto") = row.a1
        tableRow.Item("kid") = row.b1
        MergerTable.Rows.Add(tableRow)
    Next

You can play with the types if you want to do this merge with more type safety. 如果您想进行合并并获得更多类型安全性,则可以使用这些类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM