日期:2014-05-17  浏览次数:20824 次

update from子句的问题
update   t1  
  set   (Col1)   =   (   select   Col1     from   t2
                                                        where   t1.Col_Primary   =   t2.Col_Primary   );


这样把t1中有的,t2中没有的记录的Col1设成   NULL了?

------解决方案--------------------
SQL> SELECT * FROM T1;

COL1 CNAME
---------- ------------------------------
1 A
2 B
3 C

Executed in 0.157 seconds

SQL> SELECT * FROM T2;

COL1 CNAME
---------- ------------------------------
2 B
3 c
4 d

Executed in 0.14 seconds
SQL> update t1
2 set (CNAME) = ( select CNAME from t2
3 where t1.COL1 = t2.COL1);

3 rows updated

Executed in 0.141 seconds

SQL> SELECT * FROM T1;

COL1 CNAME
---------- ------------------------------
1
2 B
3 c

Executed in 0.156 seconds


SQL> update t1
2 set (CNAME) = ( select CNAME from t2
3 where t2.COL1 = t1.COL1)
4 WHERE
5 EXISTS(
6 SELECT 1 FROM T2 WHERE t1.COL1 = t2.COL1
7 ) ;

2 rows updated

Executed in 0.172 seconds

SQL> SELECT * FROM T1;

COL1 CNAME
---------- ------------------------------
1 A
2 B
3 c

Executed in 0.171 seconds


这样真是很麻烦,期待高人指点


------解决方案--------------------
是要加exists

如果数据量大的话
update很花时间