日期:2014-05-19  浏览次数:20422 次

请教一个UPDATE语句!!
表test1(f1,f2)   test2(f1,f2)
test1
f1     f2
        a
        b
        c

test2
f1   f2
1     a
2     a
3     b
4     c
5     c

现在想更新test2中的f1字段值到test1中f1字段
当test1.f2=test2.f2时
但是   当test1.f2= 'a '时,对应test2中多条记录,此时不更新
最后结果:
test1
f1       f2
          a
3         b
          c


请问怎么写????

------解决方案--------------------
update test1
set a.f1= b.f1
from test1 a ,test2 b
where a.f2 in (select f2 from test2 group by f2 having count(*)=1) and a.f2=b.f2
------解决方案--------------------
--但是 当Test1.f2= 'a '时,对应Test2中多条记录,此时不更新

--这里只针对= 'a ',还是针对所有的a、b、c

--针对所有的a、b、c,Test2中只有一条时,才跟新Test1的f1

update Test1 set f1= B.f1
from Test1 a
inner join Test2 b on A.f2=B.f2
where A.f2 in (select f2 from Test2 group by f2 having count(*)=1)

------解决方案--------------------
update test1

SET f1=isnull((select f1 from test2 a where a.f2=test1.f2 group by a.f2,f1), ' ')
where f2 in (select f2 from test2 group by f2 having count(*)=1)
------解决方案--------------------
wangtiecheng(不知不为过,不学就是错)是OK的····
------解决方案--------------------
update test1
set test1.f1=test2.f1
from test1,test2 where test2.f2 (select test1.f2 from test1) and test1.f2=teste1.f2