读书人

怎么用update from

发布时间: 2012-12-16 12:02:32 作者: rapoo

如何用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
1
2
3

如何用 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


(
id int not null,
a varchar(50) null,
b varchar(50) null,
c varchar(50) null,
d varchar(50) null
)

insert into a
select 1,'this','is','no','problem' union
select 2,'maybe','you','are','wrong' union
select 3,'please','try','align','thanks'

insert into b(id)
select 1 union
select 2 union
select 3

update b set a=a.a,b = a.b,c = a.c,d=a.d
from a
where b.id=a.id

select * from b


[其他解释]
[quote=引用:]
引用:
这上面写的方法我都试了,都不行呀!
这个可以的,不知道你错在哪一步!
额,发现错了个单词!Again
[其他解释]
试一下很期待
[其他解释]
这估计不行吧!

读书人网 >SQL Server

热点推荐