Oracle SQL – 更新第一个子字符串

huangapple go评论78阅读模式
英文:

Oracle SQL - Update first substring

问题

我想要更新 VPN 中的第一个字符串为新的 VPN,与项目匹配。所以

A124343 007-46-307-RED MEDIUM

将变成

A124343 024-46-307-RED MEDIUM

如何做到这一点?

英文:

I have the data in the column VPN like:

ITEM VPN
A124343 007-46-307-RED MEDIUM
A154363 008-25-203-YELLOW MEDIUM

I have another table UPDATED_VPN as

ITEM VPN
A124343 024
A154363 041

I want to update the first string of the VPN with the new VPN, matching the item
So

> A124343 007-46-307-RED MEDIUM

will become

> A124343 024-46-307-RED MEDIUM

how to do that?

答案1

得分: 1

你可以使用 UPDATE 语句和 EXISTSWHERE 条件中,例如

UPDATE vpn v
   SET vpn = ( SELECT u.vpn||SUBSTR(v.vpn,INSTR(v.vpn,'-')) 
                 FROM updated_vpn u 
                WHERE u.item = v.item  )
 WHERE EXISTS ( SELECT 0 FROM updated_vpn u WHERE u.item = v.item  )  

<kbd>演示</kbd>

或者,也可以使用带有 MATCHED 选项的 MERGE 语句,例如

MERGE INTO vpn v 
     USING updated_vpn u
        ON ( v.item = u.item )
      WHEN MATCHED THEN UPDATE SET v.vpn = u.vpn||SUBSTR(v.vpn,INSTR(v.vpn,'-'))

<kbd>演示</kbd>

英文:

You can use an UPDATE statement along with EXISTS within the WHERE condition
such as

UPDATE vpn v
   SET vpn = ( SELECT u.vpn||SUBSTR(v.vpn,INSTR(v.vpn,&#39;-&#39;)) 
                 FROM updated_vpn u 
                WHERE u.item = v.item  )
 WHERE EXISTS ( SELECT 0 FROM updated_vpn u WHERE u.item = v.item  )  

<kbd>Demo</kbd>

Alternatively using a MERGE statement with MATCHED option might also be used such as

MERGE INTO vpn v 
     USING updated_vpn u
        ON ( v.item = u.item )
      WHEN MATCHED THEN UPDATE SET v.vpn = u.vpn||SUBSTR(v.vpn,INSTR(v.vpn,&#39;-&#39;))

<kbd>Demo</kbd>

答案2

得分: 0

样本数据:
创建表vpns(item varchar2(10),vpn varchar2(30));
创建表updated_vpns(item varchar2(10),vpn varchar2(30));

插入全部
    到vpns值(&#39;A124343&#39;,&#39;007-46-307-RED MEDIUM&#39;)
    到vpns值(&#39;A154363&#39;,&#39;008-25-203-YELLOW MEDIUM&#39;)
    到updated_vpns值(&#39;A124343&#39;,&#39;024&#39;)
    到updated_vpns值(&#39;A154363&#39;,&#39;041&#39;)
选择*从二元组;

更新语句(也可以使用合并):
更新vpns v
设置v.vpn =
       (选择regexp_replace(v.vpn,&#39;([^-]+)&#39;,u.vpn,1,2)
         从updated_vpns u
         其中u.item = v.item);
英文:

Sample data:

create table vpns (item varchar2(10), vpn varchar2(30));
create table updated_vpns (item varchar2(10), vpn varchar2(30));

insert all
    into vpns values (&#39;A124343&#39;, &#39;007-46-307-RED MEDIUM&#39;)
    into vpns values (&#39;A154363&#39;, &#39;008-25-203-YELLOW MEDIUM&#39;)
    into updated_vpns values (&#39;A124343&#39;, &#39;024&#39;)
    into updated_vpns values (&#39;A154363&#39;, &#39;041&#39;)
select * from dual;

Update statement (could also use merge):

update vpns v
set    v.vpn =
       ( select regexp_replace(v.vpn, &#39;([^-]+)&#39;, u.vpn, 1, 2)
         from   updated_vpns u
         where  u.item = v.item );

Result:

ITEM       VPN
---------- ------------------------------
A124343    007-024-307-RED MEDIUM
A154363    008-041-203-YELLOW MEDIUM

huangapple
  • 本文由 发表于 2023年2月18日 18:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定