2012-11-20 79 views
1

我有以下兩個表 tab_1中內連接更新

FID T_NAME_1 NAME2 
--------------------- 

TAB_2

FID T_NAME_2 NAME3 
---------------------- 

對於tab_1中和tab_2的所有匹配FID有T_NAME_1和T_NAME_2的一些miss-匹配的領域,這應該不在那裏。所以我想用t_name_2的所有未匹配的值更新表tab_1的t_name_1。 我嘗試下面的查詢,其返回一個錯誤

update tab_1 set t_name_1 = ( select t2.t_name_2 from tab_2 t2 left join tab_1 t1 on 
t1.fid = t2.fid where t1.t_name_1 <> t2.t_name_2) 

回答

5

嘗試:

update tab_1 t1 
    set t_name_1 = (select t2.t_name_2 
        from tab_2 t2 
        where t1.fid = t2.fid 
         and t1.t_name_1 <> t2.t_name_2) 
where exists (select 1 
      from tab_2 t2 
     where t1.fid = t2.fid 
      and t1.t_name_1 <> t2.t_name_2) 
+0

這個工作比thanx ... – Pramod

+0

+1類似的解決方案;) – Parado

1

嘗試這種解決方案:

update tab_1 t1 set t_name_1 = ( select t2.t_name_2 
            from tab_2 t2 
            where t1.fid = t2.fid 
            and t1.t_name_1 <> t2.t_name_2) 
where exists (select * 
       from tab_2 t2 
       where t1.fid = t2.fid 
       and t1.t_name_1 <> t2.t_name_2) 
+0

返回錯誤:無法更新爲null,tab_1.t_name_1 – Pramod

+0

我錯過了一個條件。我希望它會起作用。 – Parado