0

我有两个数据表如下所示

表格1

-------------------------
  ID  |   Name 
--------------------------
 1    |  JOHN
 2    |  GEORGE
 3    |  RAGU
--------------------------

表 2

  ----------
   ID | AGE
  ----------
    1 | 23
    2 | 23
    3 | 22
  ----------

我只想要这样的结果..

结果

-------------------------
  ID  |   Name    | AGE
--------------------------
 1    |  JOHN     | 23
 2    |  GEORGE   | 23
 3    |  RAGU     | 22
--------------------------

谢谢..

4

3 回答 3

5

你可以看看这个:

static void Main(string[] args)
{
    Program p = new Program();
    DataTable dt1= p.Get1();
    DataTable dt2 = p.Get2();          
    DataTable dt3 = p.Get3(dt1, dt2);
}
public DataTable Get1()
{
    DataTable dt1 = new DataTable();
    dt1.Columns.Add("ID");
    dt1.Columns.Add("Name");
    dt1.Rows.Add("1", "JOHN");
    dt1.Rows.Add("2", "GEORGE");
    dt1.Rows.Add("3", "RAGU");        
    return dt1;
}

public DataTable Get2()
{
    DataTable dt2 = new DataTable();
    dt2.Columns.Add("AGE");         
    dt2.Rows.Add("23");
    dt2.Rows.Add("23");
    dt2.Rows.Add("22");
    return dt2;
}

public DataTable Get3(DataTable dt1,DataTable dt2)
{
    dt1.Columns.Add("Age");
    for (int i = 0; i < dt1.Rows.Count; i++)
    {
        dt1.Rows[i]["Age"] = dt2.Rows[i]["Age"];
    }
    return dt1;
}
于 2013-04-12T09:49:01.170 回答
1

你听说过INNER JOIN吗?

基本上,你想要做的是:

SELECT Persons.ID, Persons.Name, Ages.Age 
FROM Persons INNER JOIN Ages ON Persons.ID = Ages.ID

现在,您可以根据需要将其插入到另一个表中。

于 2013-04-12T09:40:18.150 回答
1

我认为这就是您可能正在寻找的

INSERT INTO Result (ID, Name, Age)
SELECT T1.ID, T1.Name, T2.Age
FROM
    Table1 AS T1
INNER JOIN
    Table2 AS T2
ON 
    T1.ID = T2.ID
ORDER BY
    T1.ID
于 2013-04-12T09:41:06.643 回答