Oracle中如何实现Mysql的两表关联update操作

在看《MySQL 5.1参考手册》的时候,发现MySQL提供了一种两表关联update操作。原文如下:

UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;

在MySQL中构造表验证了一下

复制代码
mysql> select * from test;
+------+--------+
| id   | salary |
+------+--------+
|    1 |    100 |
|    2 |    200 |
|    3 |    300 |
+------+--------+
3 rows in set (0.00 sec)

mysql> select * from test1;
+------+--------+
| id   | salary |
+------+--------+
|    1 |    400 |
|    2 |    500 |
+------+--------+
2 rows in set (0.00 sec)

mysql> update test,test1 set test.salary=test1.salary where test.id=test1.id;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from test;
+------+--------+
| id   | salary |
+------+--------+
|    1 |    400 |
|    2 |    500 |
|    3 |    300 |
+------+--------+
3 rows in set (0.00 sec)
复制代码

不难看出,上述两表关联update中只更新了test中id为1和id为2的行。

 

寻思了一下,Oracle中好像并没有提供两表关联的update操作,同样输入了上述语句,Oracle报错,报错信息如下:

SQL> update test,test1 set test.salary=test1.salary where test.id=test1.id;
update test,test1 set test.salary=test1.salary where test.id=test1.id
           *1 行出现错误:
ORA-00971: 缺失 SET 关键字

后来,查了查官方文档,这种语法并不支持,那么Oracle中如何实现MySQL中的这种效果呢?

鼓捣了一下,虽然出来了,但还是略为复杂。

复制代码
SQL> update test set test.salary=(select salary from test1 where test1.id=test.id)       
where exists (select 1 from test1 where test1.id=test.id); 已更新2行。 SQL> select * from test; ID SALARY ---------- ---------- 1 400 2 500 3 300
复制代码

 

posted @   iVictor  阅读(2397)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示