日期:2014-05-18  浏览次数:20535 次

比较多重复代码,帮忙精简下这个update的语句
SQL code

UPDATE tb1
SET name=(SELECT name FROM tb2 WHERE tb2.id=tb1.id,tb2.yy=tb1.yy,tb2.wk=tb1.wk)
,phone=(SELECT phone FROM tb2 WHERE tb2.id=tb1.id,tb2.yy=tb1.yy,tb2.wk=tb1.wk)
,addres=(SELECT addres FROM tb2 WHERE tb2.id=tb1.id,tb2.yy=tb1.yy,tb2.wk=tb1.wk)
,email=(SELECT email FROM tb2 WHERE tb2.id=tb1.id,tb2.yy=tb1.yy,tb2.wk=tb1.wk)
--
--
--
--后面还七八个都要=(SELECT *** FROM tb2 WHERE tb2.id=tb1.id and tb2.yy=tb1.yy and tb2.wk=tb1.wk)



后面那个条件一直重复,怎么写才能只需要写一遍就可以

------解决方案--------------------
SQL code
update a set
   col1=b.col1,
   col2=b.col2,
   col3=b.col3
from tb1 as a
    join tb2 as b
on a.xx=b.xx and a.xxx=b.xxx and a.xxxx=b.xxxx

------解决方案--------------------
SQL code
update
  a
set
  name=b.name,phone=b.phone...
from
  tb1 a join tb2 b
on
  a.id=b.id
and
  a.yy=b.yy and a.wk=b.wk