有两个表t1,t2,结构完全一样。现在要用t1表里的数据更新t2表里的数据,更新的原则是如果前两个字段相同,则只更新最后一个字段。用一句SQL语句怎么才能搞定?
t1(code varchar(10),telephone varchar(15),address varchar(50))
t2(code varchar(10),telephone varchar(15),address varchar(50))
还望各位大虾赐教!多谢!
t1(code varchar(10),telephone varchar(15),address varchar(50))
t2(code varchar(10),telephone varchar(15),address varchar(50))
还望各位大虾赐教!多谢!
create table t2(code varchar(10),telephone varchar(15),address varchar(50))insert into t1 select '1001','898','dddddd'
insert into t1 select '1002','777','dddddd'
insert into t1 select '1003','777','dddddd'
insert into t2 select '1001','666','aa'
insert into t2 select '1002','777','daaa'
insert into t2 select '1003','666','daaa'select * from t2
goupdate b set b.address=a.address from t1 a,t2 b where a.code=b.code and a.telephone=b.telephone
goselect * from t2drop table t1
drop table t2
update b set address=address from t1 a,t2 b where a.code=b.code and a.telephone=b.telephone
update b set b.code=a.code and b.telephone=a.telephone and b.address=a.address from t1 a,t2 b where a.code<>b.code or a.telephone<>b.telephone;
就是你理解的这个意思!但是能不能用一条SQL语句搞定啊?
Update t2 Set address=t1.address from t2,t1 where t1.code=t2.code and t1.telephone=t2.telephone你可以查下MS SQL的联机文档,如果我没有记错的话应该上面那么写.自己测试下吧.
from t1,t2
where t1.code = t2.code
and t1.telephone = t2.telephone