Oracle SQL 相关更新

Oracle SQL correlated update(Oracle SQL 相关更新)
本文介绍了Oracle SQL 相关更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三张桌子:

t1.columns: a,c
t2.columns: a,b
t3.columns: b,c,d

现在我想要用 t3.d 更新 t1.c.但我不能只使用 t1.c = t3.c 从 t3 更新 t1 我还必须通过 t3.b = t2.b 和 t1.a = t2.a.

Now what I want is to update t1.c with t3.d. But I can't just update t1 from t3 using t1.c = t3.c I also have to go though t3.b = t2.b and t1.a = t2.a.

我尝试过这样的事情:

UPDATE table1 t1
   SET t1.c = (select t3.d
               from table2 t2, table3 t3
               where t2.b = t3.b and t1.a = t2.a)                                  
 WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);

此代码生成错误消息:ORA-01427:单行子查询返回多于一行

This code generates error-msg: ORA-01427: single-row subquery returns more than one row

推荐答案

如果 t1 和 t2 之间或 t2 和 t3 之间存在一对多关系,则 t1 中的每一行都会有很多匹配项.如果您知道 t3 中属于 t1 中同一行的所有行在 d 中具有相同的值,那么您可以使用 DISTINCT 删除(相同的)重复项.

If there is a one-to-many relationship between t1 and t2 or between t2 and t3 you will get many matches for each row in t1. If you know that all rows in t3 that belong to the same row in t1 have the same value in d, then you can use DISTINCT to remove (identical) duplicates.

UPDATE table1 t1
   SET t1.c = (select DISTINCT t3.d
               from table2 t2, table3 t3
               where t2.b = t3.b and t1.a = t2.a)                                  
 WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);

这篇关于Oracle SQL 相关更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)