英文:
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
语句和 EXISTS
在 WHERE
条件中,例如
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,'-'))
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,'-'))
<kbd>Demo</kbd>
答案2
得分: 0
样本数据:
创建表vpns(item varchar2(10),vpn varchar2(30));
创建表updated_vpns(item varchar2(10),vpn varchar2(30));
插入全部
到vpns值('A124343','007-46-307-RED MEDIUM')
到vpns值('A154363','008-25-203-YELLOW MEDIUM')
到updated_vpns值('A124343','024')
到updated_vpns值('A154363','041')
选择*从二元组;
更新语句(也可以使用合并):
更新vpns v
设置v.vpn =
(选择regexp_replace(v.vpn,'([^-]+)',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 ('A124343', '007-46-307-RED MEDIUM')
into vpns values ('A154363', '008-25-203-YELLOW MEDIUM')
into updated_vpns values ('A124343', '024')
into updated_vpns values ('A154363', '041')
select * from dual;
Update statement (could also use merge):
update vpns v
set v.vpn =
( select regexp_replace(v.vpn, '([^-]+)', 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论