使用子子查询更新 SQL
我在 Oracle 11g 中遇到以下更新查询问题:
update TABLE_A a set COL1 =
(SELECT b.COL2 FROM
(SELECT ROWNUM AS ROW_NUMBER, b.COL2 from TABLE_B b where COL3 = a.COL4)
WHERE ROW_NUMBER = 2
)
ORA-00904: "A"."COL4": invalid ID 。
因此,a.COL4 在子子查询中是未知的,但我不知道如何解决这个问题。
/编辑。我想做什么?
TABLE_A 中的每条记录在 TABLE_B 中都有多个记录。然而,来自客户的新要求:TABLE_A 将获得 2 个新列,而 TABLE_B 将被删除。因此,子查询的第一条记录的表示将被写入第一个新字段,第二个新字段也是如此。第一条记录很简单,因为 Mike C 的解决方案可以与 ROW_NUMBER = 1 一起使用。
示例行:
TABLE_A
| col0 | col1 | col2 | col3 | col4 |
------------------------------------
| | |dummy2|dummy3| 1 |
------------------------------------
| | |dummy4|dummy5| 2 |
------------------------------------
TABLE_B
| col1 | col2 | col3 |
----------------------
| d |name1 | 1 |
----------------------
| d |name2 | 1 |
----------------------
| d |name3 | 1 |
----------------------
| d |name4 | 2 |
----------------------
TABLE_A after update
| col0 | col1 | col2 | col3 | col4 |
------------------------------------
| name1| name2|dummy2|dummy3| 1 |
------------------------------------
| name4| |dummy4|dummy5| 2 |
------------------------------------
I'm having a problem with the following update query in Oracle 11g:
update TABLE_A a set COL1 =
(SELECT b.COL2 FROM
(SELECT ROWNUM AS ROW_NUMBER, b.COL2 from TABLE_B b where COL3 = a.COL4)
WHERE ROW_NUMBER = 2
)
ORA-00904: "A"."COL4": invalid ID .
So, a.COL4 is not known in the subsubquery, but I don't have an idea how to solve this.
/Edit. What am I trying to do?
There are multiple records in TABLE_B for every record in TABLE_A. New requirements from the customer however: TABLE_A will get 2 new columns instead, while TABLE_B will be deleted. So a representation of the first record of the subquery will be written to the first new field and the same for the second one. First record is easy, since Mike C's solution can be used with ROW_NUMBER = 1.
Example rows:
TABLE_A
| col0 | col1 | col2 | col3 | col4 |
------------------------------------
| | |dummy2|dummy3| 1 |
------------------------------------
| | |dummy4|dummy5| 2 |
------------------------------------
TABLE_B
| col1 | col2 | col3 |
----------------------
| d |name1 | 1 |
----------------------
| d |name2 | 1 |
----------------------
| d |name3 | 1 |
----------------------
| d |name4 | 2 |
----------------------
TABLE_A after update
| col0 | col1 | col2 | col3 | col4 |
------------------------------------
| name1| name2|dummy2|dummy3| 1 |
------------------------------------
| name4| |dummy4|dummy5| 2 |
------------------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你能像这样消除一个子查询吗?
Can you eliminate one of the subqueries like this?
尝试
我假设 COL3 来自表 b,为什么还要在子查询中选择 ROWNUM ? WHERE 子句中的值只能是 2。
Try
I'm assuming COL3 comes from table b, also why are you including selecting ROWNUM in the subquery? It can only be 2 from your WHERE clause.
我认为这可能是解决您问题的一个可能的解决方案,但根据您正在处理的数据量,它可能会非常慢,因为内部语句没有限制因素。
I think this could be a possible solution to your problem but depending on the amount of data you're processing it could be really slow because there is no limiting factor for the inner statement.
我使用临时表解决了这个问题,当表 A 填满时从中删除数据。
I solved this using a temporary table, deleting data from it as table A gets filled.