繁体   English   中英

从 Oracle 中的另一个表更新表上的键

[英]Updating a key on table from another table in Oracle

当键值为 (abc) 时,我试图通过从表 (t2) 获取值来更新表 (t1) 上的键。

当我将其限制为特定人员时,它按预期工作

update table_a t1
   set t1.u_key = (select t2.u_key 
                   from table_b t2 
                   where t2.name_f=t1.name_f 
                     and t2.name_l=t1.name_l 
                     and rownum<=1 
                     and t2='NEVADA')
where t1.u_key = 'abc'
and e.name_f='Lori' 
and e.name_l='U'
;

我最初尝试不使用 rownum,它说返回的行太多。

为了使用 t1.u_key='abc' 运行所有数据并取出特定名称,我尝试了这个一直运行到超时的方法。

update table_a t1
   set t1.u_key = (select t2.u_key 
                   from table_b t2 
                   where t2.name_f=t1.name_f 
                     and t2.name_l=t1.name_l 
                     and rownum<=1 
                     and t2='NEVADA')
where t1.u_key = 'abc'
;

你能看看它并建议我错过什么吗?

您应该首先查看单独运行内部 SELECT 语句时返回的内容:

SELECT t2.u_key FROM table_b t2 
WHERE t2.name_f IN (SELECT name_f FROM table_a WHERE u_key = 'abc') 
AND t2.name_l IN (SELECT name_l FROM table_a WHERE u_key = 'abc') 
AND t2='NEVADA'

检查结果,您将看到返回的行不止一行。

如果每个键应该只有匹配的行,您还需要将键添加到内部 SELECT 中,但我无法告诉您如果没有额外的表描述以及可能来自table_atable_b一些示例条目,它应该是什么样子。

用这个:

update     ( 
             SELECT t2.u_key t2key,
                    t1.ukey t1key
              FROM table_b t2,
                   table_a t1
              where t2.name_f=t1.name_f 
              and t2.name_l=t1.name_l                
              and t2='NEVADA'
              and rownum<=1 )
SET     t1key =     t2key
where   t1key = 'abc';   
merge into table_a t1
 using(
       select name_f, name_l, max(u_key) as new_key
         from table_b t2 
        where t2='NEVADA'
        group by name_f, name_l
      ) t2
   on (t1.name_f=t2.name_f and t1.name_l=t2.name_l and t1.u_key='abc')
 when matched then
  update set t1.u_key=t2.new_key

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM