Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle SQL:使用第三个表引用的另一个表更新表列_Sql_Oracle_Sql Update - Fatal编程技术网

Oracle SQL:使用第三个表引用的另一个表更新表列

Oracle SQL:使用第三个表引用的另一个表更新表列,sql,oracle,sql-update,Sql,Oracle,Sql Update,我目前有以下三个表格: 表1 book_id margin ------------------- b1 10 b2 20 b3 30 表2 t2_id book_id author_id ----------------------------- 1 b1 100 2 b2 200 3 b3 300 表3 author

我目前有以下三个表格:

表1

book_id    margin
-------------------
   b1       10
   b2       20
   b3       30
表2

t2_id   book_id     author_id
-----------------------------
  1         b1        100
  2         b2        200
  3         b3        300
表3

author_id    revenue
----------------------
   100          0
   200          0
   300          0
我试图更新表3中的收入,将该书的对应作者(表3)从表1中获得50%的利润。结果应将表3更新为:

author_id    revenue
----------------------
   100          10
   200          20
   300          30

我可以从另一个表中更新值,如果它们与一个公共密钥直接链接在一起,我很难在中间引用另一个表来得到答案:(

) 我试过:

UPDATE table3 t3 SET revenue = 
(SELECT t1.margin FROM table1 t1 WHERE
(SELECT t1.book_id FROM table1 t1 JOIN table2 t2 ON t1.book_id = t2.book_id) = 
(SELECT author_id FROM table3 t3 JOIN table2 t2 ON t3.authoer_id = t2.author_id));

谢谢

使用
更新加入

update 
(
SELECT table3.revenue as OLD, table1.margin as NEW
FROM table1 INNER JOIN table2 on table1.book_id=table2.book_id
inner join table3 on table2.author_id=table3.author_id
)t set t.old=t.new

对子查询使用合并更新

 MERGE INTO table3 t3 
 using 
 (select t1.margin,t2.author_id
 from tabl1 t1 join table2 t2 on t1.book_id=t2.book_id
  ) a ON (t3.author_id = a.author_id)
 when matchced then
 update SET t3.revenue = a.margin

您也可以通过使用
with..as
方法来使用
update

update table3 t3
set t3.revenue = 
    (with t as (
                 select * 
                   from table1 t1
                   join table2 t2 on t2.book_id = t1.book_id
                )
                select t.margin from t where t.author_id = t3.author_id);

谢谢,您的代码运行得很好,但显然,我的数据还有很多问题,例如需要groupby和groupby表返回空值等等……啊