使用子子查询更新 SQL

发布于 2024-12-05 20:50:49 字数 1253 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮云落日 2024-12-12 20:50:50
UPDATE TABLE_A a SET COL1 = 
  (SELECT b.COL2 FROM
    (SELECT ROWNUM AS ROW_NUMBER, b.COL2 FROM TABLE_B b, TABLE_A innerA WHERE COL3 = innerA.COL4) 
   WHERE ROW_NUMBER = 2
  )
UPDATE TABLE_A a SET COL1 = 
  (SELECT b.COL2 FROM
    (SELECT ROWNUM AS ROW_NUMBER, b.COL2 FROM TABLE_B b, TABLE_A innerA WHERE COL3 = innerA.COL4) 
   WHERE ROW_NUMBER = 2
  )
仙女 2024-12-12 20:50:50

你能像这样消除一个子查询吗?

update TABLE_A a set COL1 = 
(SELECT b.COL2 FROM TABLE_B b where COL3 = a.COL4 AND ROWNUM = 2)

Can you eliminate one of the subqueries like this?

update TABLE_A a set COL1 = 
(SELECT b.COL2 FROM TABLE_B b where COL3 = a.COL4 AND ROWNUM = 2)
烟酒忠诚 2024-12-12 20:50:50

尝试

update TABLE_A a set COL1 = 
  (SELECT b.COL2 FROM
    (SELECT ROWNUM AS ROW_NUMBER, b.COL2 from TABLE_B b, TABLE_A a2 where b.COL3 = a2.COL4) 
   WHERE ROW_NUMBER = 2
  )

我假设 COL3 来自表 b,为什么还要在子查询中选择 ROWNUM ? WHERE 子句中的值只能是 2。

Try

update TABLE_A a set COL1 = 
  (SELECT b.COL2 FROM
    (SELECT ROWNUM AS ROW_NUMBER, b.COL2 from TABLE_B b, TABLE_A a2 where b.COL3 = a2.COL4) 
   WHERE ROW_NUMBER = 2
  )

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.

远昼 2024-12-12 20:50:50

我认为这可能是解决您问题的一个可能的解决方案,但根据您正在处理的数据量,它可能会非常慢,因为内部语句没有限制因素。

update
  table_a upd
set upd.col1 = (
  select
    sub.col2
  from
    (
      select
        rownum as row_number,
        b.col2 as col2,
        b.col3 as col3
      from
        table_a a,
        table_b b
      where b.col3 = a.col4
    ) sub
  where sub.row_number = 2
    and sub.col3       = upd.col4
)

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.

update
  table_a upd
set upd.col1 = (
  select
    sub.col2
  from
    (
      select
        rownum as row_number,
        b.col2 as col2,
        b.col3 as col3
      from
        table_a a,
        table_b b
      where b.col3 = a.col4
    ) sub
  where sub.row_number = 2
    and sub.col3       = upd.col4
)
夏日浅笑〃 2024-12-12 20:50:50

我使用临时表解决了这个问题,当表 A 填满时从中删除数据。

I solved this using a temporary table, deleting data from it as table A gets filled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文