1

我需要从另一个表更新一些数据..

我想简单地将table1中col1的数据带到table2中的col1,这些行位于相同的位置,所以实际上不需要进行任何id-comparing ..

我怎样才能做到这一点?

-----------------------------------------
Db1.Table1 (Take yearModel from this table to update Table3)
-----------------------------------------
imgid          | int | PK |
yearModel      | int |
-----------------------------------------


-----------------------------------------
Db2.Table2 (Go by this table to update Table3)
-----------------------------------------
imgid          | int | PK |
uploadId       | int | FK (With table3.uploadId) |
------------------------------------------

------------------------------------------
Db2.Table3 (Update this table with the yearModel from table1)
-------------------------------------------
uploadId        | int | PK |
uploadYearModel | int |

抱歉,我的数据库图表“thingy”由于某种奇怪的原因而崩溃了.. :( 我希望你能理解这个想法

我还应该提到 Table1 位于另一个数据库中..我不确定这是否重要..但好吧.. :)

提前致谢!

4

3 回答 3

2

如果您在两个表中都有一个公共键列并且此键列在两个表中具有相同的值(它们在两个表中引用相同的行),那么下面的查询应该可以工作:

UPDATE  Table1
SET     Column1 = t2.Column1
FROM    Table2 t2 INNER JOIN Table1 t1 ON t2.KeyColumn = t1.KeyColumn

编辑:

UPDATE Table3 
SET    uploadYearModel = t1.yearModel 
FROM   Table1 t1
       INNER JOIN Table2 t2 ON t2.imgid = t1.imgid
       INNER JOIN Table3 t3 ON t3.uploadId = t2.uploadId
于 2012-07-10T09:13:01.037 回答
0
UPDATE table2 SET col1=(
       SELECT top 1 col1 FROM table1 WHERE table1.id=table2.id
)
于 2012-07-10T09:11:57.553 回答
0

要更新现有记录:

UPDATE table2 
SET table2.c1 = table1.c1, 
table2.c2 = table1.c2,
table2.c3 = table1.c3
FROM table1, table2 
WHERE table1.c1 = table2.c1
于 2012-07-10T09:26:32.987 回答