有两张表
现在要更新t2中的file1字段,使 t2.file1的字段值 等于t1.id的值,其中t1和t2通过 t1.file2 和t2.file2相关联,t2的数据量要大于t1中的数据量,如果在t2中找不到对应的值,则不更新。
这个更新语句改怎么写? 
                  
    t1                       t2              
id     file2  file3       file1   file2      
1        1      2           1        1
2        2      3           2        3  
3        3      2           1        2
                            3        4             

解决方案 »

  1.   


    update t2 a set file1 = (select id from t1 where file2=a.file2) ;
      

  2.   

    update t2 a set a.file1 = (select max(b.file1) from t1 b where b.file2=a.file2 group by b.file2 having max(b.file1)>a.file1 )
    where exists (select 1 from t1 b where b.file2=a.file2 and b.file1>a.file1 ) ;
      

  3.   

    是t2的记录数要大于t1的记录数 就是说 t2中的数据在t1中不一定有对应的值,就不用修改file2的值
      

  4.   

    update t2 a set file1=(select id from t1 b where a.filed2=b.filed2)
    where exists(select 1 from t1 c where a.filed2=c.filed2)
      

  5.   

    问题解决了 不过需要在第一个查询后面加上rownum=1
    update t2 a set file1=(select id from t1 b where a.filed2=b.filed2 and rownum=1)
    where exists(select 1 from t1 c where a.filed2=c.filed2)