在另一个表的表中插入值

时间:2013-06-24 11:29:48

标签: c# sql-server

让某人帮助我将表格中的值添加到sql server

中的另一个表格中

我有一个列(reserve,name,price)

的表

另一个列(name,price)

的表格

我想从table1(price)

插入table2(price)

5 个答案:

答案 0 :(得分:2)

INSERT INTO TABLE1(Price) 
(
    SELECT Price FROM TABLE2
)

答案 1 :(得分:2)

张贴回答:

如果您尝试将Table1中的price列更新为Table2(Price)中的值:

UPDATE TBL1
SET    TBL1.Price = TBL2.Price
FROM   Table1 TBL1
INNER JOIN Table2 TBL2 ON TBL1.name = TBL2.name

答案 2 :(得分:1)

using (SqlConnection con= new SqlConnection(connectionString))
{ 
 MySqlCommand cmd = new MySqlCommand("INSERT INTO table2(name,price) SELECT name,price from table1", con);
 con.Open();
 cmd.ExecuteNonQuery();
}

答案 3 :(得分:1)

查询:将数据从一个表格插入另一个表格

1)使用Insert into:在早先在数据库中创建表并且将数据从另一个同时具有 相同模式的表插入此表时使用

 insert into tb1 (name,price) select name,price from tb2;

2)使用Select into:仅当INTO子句中指定的表不存在时才使用它。它将创建与所选列相同的数据类型的新表。

select name,price into tb3 from tb2;

<强> DEMO SQL FIDDLE

答案 4 :(得分:0)

insert into table1(price) select price from table2