Oracle SQL: Update a table with data from another table(Oracle SQL:用另一个表中的数据更新一个表)
问题描述
表 1:
id name desc
-----------------------
1 a abc
2 b def
3 c adf
表 2:
id name desc
-----------------------
1 x 123
2 y 345
在 oracle SQL 中,如何运行 sql update 查询,该查询可以使用相同的表 2 的 name 和 desc 更新表 1id?所以我得到的最终结果是
In oracle SQL, how do I run an sql update query that can update Table 1 with Table 2's name and desc using the same id? So the end result I would get is
表 1:
id name desc
-----------------------
1 x 123
2 y 345
3 c adf
问题来自用另一个表更新一个表,但专门用于 oracle SQL.
Question is taken from update one table with data from another, but specifically for oracle SQL.
推荐答案
这称为相关更新
UPDATE table1 t1
SET (name, desc) = (SELECT t2.name, t2.desc
FROM table2 t2
WHERE t1.id = t2.id)
WHERE EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.id = t2.id )
假设联接结果为保留键的视图,您还可以
Assuming the join results in a key-preserved view, you could also
UPDATE (SELECT t1.id,
t1.name name1,
t1.desc desc1,
t2.name name2,
t2.desc desc2
FROM table1 t1,
table2 t2
WHERE t1.id = t2.id)
SET name1 = name2,
desc1 = desc2
这篇关于Oracle SQL:用另一个表中的数据更新一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle SQL:用另一个表中的数据更新一个表
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
