内部联接更新

时间:2012-11-20 07:39:22

标签: sql oracle11g

我有两张桌子 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不匹配的字段,这些字段不应该存在。所以我想更新表tab_1的t_name_1,其中包含所有未匹配的t_name_2值。 我尝试了以下返回错误的查询

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)

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)