sql更新语句

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:11:46
There are 2 tables as follows:
dmSQL> select * from t1;
C1 C2
=========== ===========
1 1
2 2
3 3
3 rows selected
dmSQL> select * from t2;
C1 C2
=========== ===========
1 11
3 33
2 rows selected
Please submit a SQL command to update t1. This new updated t1 will look as follows:
dmSQL> select * from t1;
C1 C2
=========== ===========
1 11
2 2
3 33
3 rows selected
Note: Only the rows existing in table2 will be replaced, or else it will keep the original value.
为什么Update t1 t2 set t1.c2=t2.c2 where t1.c1=t2.c1;不行啊

Update t1 t2
update 后面接你要更新的那个表
update t1 set t1.c2=t2.c2 where t1.c1=t2.c1;

不行吗?
你试试:
update t1 set c2=(select t2.c2 from t2 where t1.c1=t2.c1) where c1 in
(selct c1 from t2);

Update t1 set t1.c2=t2.c2 where t1.c1=t2.c1

update t1 set t1.c2 =(select top 1 t2.c2 from t1,t2 where t1.c1=t2.c1 order by t2.c2 desc )

update 后面只能跟一个表
Update t1 set t1.c2=t2.c2 from t1 inner join
t2 on t1.c1=t2.c1

合并语句
merge into t1 --目标数据表
using t2 -- 源数据表
on(t1.c1 = t2.c1) --连接条件
when matched then --当条件满足时
update set t1.c2 = t2.c2 -- --更新
when not matched then --当条件不满足时
insert values(t2.c1,t2.c2) --插入

update 语句本身只能一次更新一个表,想要同时作,可以写成存储过程,或者上面的合并豫剧。