SQL> select *from a1;
ID N SFZHAO
---------- - ------------------
1 a
1 c
2 a
2 c
3 c 123
SQL> select *from a2;
ID N SFZHAO
---------- - ------------------
1 c 111
2 d 111
--如果使用下面的sql来更新表a1的话,我们发现出现了我们不希望出现的结果,把a1中id=3所对应的sfzhao由原来的123更新成了null,这是我们不希望的。
SQL> update a1 set sfzhao=(select a2.sfzhao from a2 where a2.id=a1.id and a2.nam
e=a1.name);
已更新5行。
SQL> select *from a1;
ID N SFZHAO
---------- - ------------------
1 a
1 c 111
2 a
2 c
3 c
SQL> rollback;
回退已完成。
--为了纠正上面update所产生的错误结果,在where字句中做了进一步的限定。
SQL> update a1 set sfzhao=(select a2.sfzhao from a2 where a2.id=a1.id and a2.nam
e=a1.name) where (id , name) in (select id , name from a2 where a2.id=a1.id and
a2.name=a1.name);
已更新 1 行。
SQL> select *from a1;
ID N SFZHAO
---------- - ------------------
1 a
1 c 111
2 a
2 c
3 c 123
SQL>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/19602/viewspace-1014388/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/19602/viewspace-1014388/