日期:2014-05-17  浏览次数:20529 次

如何用update from
TABLE 1
ID C1 C2  C3
1  XX  YY  ZZ
2  XX1  YY1  ZZ1
3  XX2  YY2  ZZ2


TABLE 2  把C1C2C3 通过表1 放进去
ID C1 C2  C3




如何用 update from啊

我写的是 
update table2  set c1=(select top 1 c1 from table1 where id =table2.id),
 c2=(select top 1 c2 from table1 where id =table2.id)
 c3=(select top 1 c3 from table1 where id =table2.id)

这种方法很死,而且没有效率

我想用
update table2 
set c1=t.c1,c2=t.c2,c3=t.c3
from  (select top 1 c1,c2,c3 from table1 where id=table1.id ) t

为什么不行。报错。。。
------最佳解决方案--------------------
UPDATE  b
SET     b.c1 = a.c1 ,
        b.c2 = a.c2 ,
        b.c3 = a.c3
FROM    TABLE1 a
        INNER JOIN TABLE2 b ON a.id = b.id

------其他解决方案--------------------
update table2 set C1=b.c1,c2=b.c2,c3=b.c3
from table2 as a inner join table1 as b on a.id=b.id
------其他解决方案--------------------
update table2 
set c1=t.c1,c2=t.c2,c3=t.c3
from  (select top 1 c1,c2,c3 from table1 where id=table1.id ) t

此句错误在于,select的值不确定,换句话讲范围不定啊。你要找一个唯一确定的值。
------其他解决方案--------------------
update table_2 set c1=a.c1,c2 = a.c2,b.c3 = a.c3 
from  table_1 a
where table_2.id=a.id

------其他解决方案--------------------
引用:
SQL code?
123
update table_2 set c1=a.c1,c2 = a.c2,b.c3 = a.c3  from  table_1 a where table_2.id=a.id

update table_2 set c1=a.c1,c2 = a.c2,c3 = a.c3 
from  table_1 a
where table_2.id=a.id
写错一个字母!
------其他解决方案--------------------
update table2 set C1=b.c1,c2=b.c2,c3=b.c3
from table2 as a inner join table1 as b on a.id=b.id 

------其他解决方案--------------------
这上面写的方法我都试了,都不行呀!
------其他解决方案--------------------
引用:
这上面写的方法我都试了,都不行呀!

这个可以的,不知道你错在哪一步!
create table a
(
 id int not null,
 a varchar(50) null,
 b varchar(50) null,
     c varchar(50) null,
     d varchar(50) null
)

create table b