1

我有以下两个表 TAB_1

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

选项卡_2

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

对于 tab_1 和 tab_2 的所有匹配 fid,T_NAME_1 和 T_NAME_2 的一些不匹配字段不应该存在。所以我想用 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)
4

2 回答 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)
于 2012-11-20T07:48:34.657 回答
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)
于 2012-11-20T07:41:23.673 回答