SQL> select * from a2;BBB CCC ---------- -------------------- 开发者 SQL> select * from a4;BBB CCC ---------- -------------------- 开 kai 发 fa 者 zhe SQL> update a2 set a2.ccc=( 2 select ccc1||ccc2||ccc3 from ( 3 select a4.ccc ccc1 from a4,a2 where instr(a2.bbb,a4.bbb)=1), 4 (select a4.ccc ccc2 from a4,a2 where instr(a2.bbb,a4.bbb)=2), 5 (select a4.ccc ccc3 from a4,a2 where instr(a2.bbb,a4.bbb)=3)) 6 where a2.bbb= '开发者';已更新 1 行。 SQL> select * from a2;BBB CCC ---------- -------------------- 开发者 kaifazhe
我是这样写的 update a set xm=(select b1.dm||b2.dm||b3.dm from b b1,b b2,b b3 where substr(xm,1,1)=b1.hz and substr(xm,2,1)=b2.hz and substr(xm,3,1)=b3.hz) 但是这样出现一个问题,就是在姓名为两个字时,因为子查询中的b3.dm返回null,所以返回的xm为空。
写个过程很简单,何必追求一个sql,即使可以实现,也未必是最好的解决方案.
which(which)的改一下就可以了 名字至少也要两个把,加个判断update a set xm=(select b1.dm||b2.dm||decode(b3.dm,null,'',b3.dm) from b b1,b b2,b b3 where substr(xm,1,1)=b1.hz and substr(xm,2,1)=b2.hz and substr(xm,3,1)=b3.hz)
---------- --------------------
开发者
SQL> select * from a4;BBB CCC
---------- --------------------
开 kai
发 fa
者 zhe
SQL> update a2 set a2.ccc=(
2 select ccc1||ccc2||ccc3 from (
3 select a4.ccc ccc1 from a4,a2 where instr(a2.bbb,a4.bbb)=1),
4 (select a4.ccc ccc2 from a4,a2 where instr(a2.bbb,a4.bbb)=2),
5 (select a4.ccc ccc3 from a4,a2 where instr(a2.bbb,a4.bbb)=3))
6 where a2.bbb= '开发者';已更新 1 行。
SQL> select * from a2;BBB CCC
---------- --------------------
开发者 kaifazhe
update a set xm=(select b1.dm||b2.dm||b3.dm from b b1,b b2,b b3 where substr(xm,1,1)=b1.hz and substr(xm,2,1)=b2.hz and substr(xm,3,1)=b3.hz)
但是这样出现一个问题,就是在姓名为两个字时,因为子查询中的b3.dm返回null,所以返回的xm为空。
名字至少也要两个把,加个判断update a set xm=(select b1.dm||b2.dm||decode(b3.dm,null,'',b3.dm) from b b1,b b2,b b3 where substr(xm,1,1)=b1.hz and substr(xm,2,1)=b2.hz and substr(xm,3,1)=b3.hz)
你的办法就可以呀!
或者分批处理不同字数的名字,用NVL处理NULL也可以。