使用另一列中的值更新列

发布于 2024-08-03 03:03:22 字数 680 浏览 5 评论 0原文

我有一个像这样的表:

create table foo ( a number, b number ) 

我想用另一个表中的值更新 a 中的所有列

create table bar ( x number, y number ) 

所以,如果这是一种过程编程语言,我会:

 foreach foo_item in foo 
     foreach bar_item in bar 
         if( foo_item.b == bar_item.y ) 
             foo_item.a = bar_item.x 
         end
     end
 end

我已经尝试过

update foo 
set a = ( select distinct( x ) from bar where bar.y = foo.b ) 

但它挂起...... 我不太确定如何做这样的事情(甚至不知道要谷歌搜索什么)

谢谢

编辑抱歉我的错。它没有挂起,但它尝试设置 va null 值,并且我有一个约束(我无法删除)

感谢到目前为止的帮助

I have a table like this:

create table foo ( a number, b number ) 

I want to update all the columns from a with the value that is in another table

create table bar ( x number, y number ) 

So, if this would be a procedural programing language I would:

 foreach foo_item in foo 
     foreach bar_item in bar 
         if( foo_item.b == bar_item.y ) 
             foo_item.a = bar_item.x 
         end
     end
 end

I have tried

update foo 
set a = ( select distinct( x ) from bar where bar.y = foo.b ) 

But it hangs.... I'm not really sure how to do such a thing ( or even what to google for )

Thanks

EDIT Sorry my bad. It doesn't hang, but it tries to set va null value and I have a constraint ( which I can't remove )

Thanks for the help so far

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

触ぅ动初心 2024-08-10 03:03:22

尝试将 foo.a 更新为 NULL 有两个可能的原因。

  1. foo 中存在一些行,而 bar 中没有匹配的行。
  2. bar 中的匹配行的 bar.x 为 null。

如果上述任一条件为真,则以下内容将排除对 foo 的更新。在这些情况下 foo.a 将保持原样:

update foo 
set a = (select distinct( x ) from bar where bar.y = foo.b )
where exists 
  (select *
  from bar 
  where bar.y = foo.b
  and bar.x is not null);

There are two possible reasons for the update to attempting to foo.a to NULL.

  1. There exists rows in foo for which there is no matching rows in bar.
  2. The matching row in bar has bar.x of null.

The following will exclude updates to foo if either of the above conditions is true. In those cases foo.a will remain as it was:

update foo 
set a = (select distinct( x ) from bar where bar.y = foo.b )
where exists 
  (select *
  from bar 
  where bar.y = foo.b
  and bar.x is not null);
小糖芽 2024-08-10 03:03:22

这会失败/旋转:

UPDATE foo 
   SET b = (SELECT DISTINCT(x) 
              FROM bar 
             WHERE bar.y = foo.b)

...因为您正在更新您想要用来确定更新内容的相同值。 Oracle 始终允许用户读取数据。

This fails/spins:

UPDATE foo 
   SET b = (SELECT DISTINCT(x) 
              FROM bar 
             WHERE bar.y = foo.b)

...because you are updating the same value you want to use to determine what to update with. Oracle always allows a user to read data.

梅倚清风 2024-08-10 03:03:22

更新 foo set b = ( 选择不同(
x ) 来自 bar,其中 bar.y = foo.b)

可能会因性能原因而挂起,但应该可以工作。仔细检查如果没有 bar.y 等于 foo.b 会发生什么。如果将b设置为null可以吗?

update foo set b = ( select distinct(
x ) from bar where bar.y = foo.b )

May hang for performance reasons but should work. Double check what happends if there is no bar.y equal to foo.b. If it sets b to null is OK?

橪书 2024-08-10 03:03:22

在您提供的查询中,您似乎有一个拼写错误。在过程代码中,您修改了 foo.a 的值,但查询更新了 foo.b:

update foo set a = ( select distinct( x ) from bar where bar.y = foo.b )

此外,如果有许多行具有相同的 bar.y 值,则可能会出现问题。您的子查询可能返回一个结果集,而不是您的赋值所期望的单个值。
例如,如果您的数据是

foo(x,y) = [{1,2},{2,2},{3,2}]

那么“DISTINCT x”将返回“{1,2,3}

In the query you provided you seem to have a typo. In your procedural code you modify the value of foo.a, but your query updates foo.b:

update foo set a = ( select distinct( x ) from bar where bar.y = foo.b )

Also, if there are many rows with identical values for bar.y, problems may occur. Your subquery may return a result set, not a single value that your assignment expects.
For example if your data is

foo(x,y) = [{1,2},{2,2},{3,2}]

Then "DISTINCT x" will return '{1,2,3}'

柒夜笙歌凉 2024-08-10 03:03:22

假设您有以下值。

foo(a,b) = [{0,2}]
bar(x,y) = [{1,2},{2,2},{3,2}]

上面给出的答案引发了 ORA-01427 错误。该语句需要一些补充,这取决于预期的结果。
如果您期望最大的 bar(x,2) 应该存储在 foo(a,2) 中。

foo(a,b) = [{3,2}]
update foo 
  set a = (select max(x) from bar where bar.y = foo.b  
           and bar.x is not null)
where exists 
  (select *
  from bar 
  where bar.y = foo.b
  and bar.x is not null);

如果您期望 bar(x,2) 为任何值,请编写以下内容。

foo(a,b) = [{[1|2|3],2}]
update foo 
  set a = (select x from bar where bar.y = foo.b 
           and bar.x is not null 
           and rownum < 2)
where exists 
  (select *
  from bar 
  where bar.y = foo.b
  and bar.x is not null);

子选择的顺序取决于存储和行检索。两次更新可以给出相同的结果。如果没有 ORDER BY,则行顺序是不可预测的。 rownum 2 仅获取子选择的第一行。

Assume you have following values.

foo(a,b) = [{0,2}]
bar(x,y) = [{1,2},{2,2},{3,2}]

The answer given above raises an ORA-01427 error. The statement need some additions, which depend on the expected result.
If you expect that the greatest bar(x,2) should be stored in foo(a,2).

foo(a,b) = [{3,2}]
update foo 
  set a = (select max(x) from bar where bar.y = foo.b  
           and bar.x is not null)
where exists 
  (select *
  from bar 
  where bar.y = foo.b
  and bar.x is not null);

If you expect any value of bar(x,2) than write following.

foo(a,b) = [{[1|2|3],2}]
update foo 
  set a = (select x from bar where bar.y = foo.b 
           and bar.x is not null 
           and rownum < 2)
where exists 
  (select *
  from bar 
  where bar.y = foo.b
  and bar.x is not null);

The order of the subselect depends on storage and row retrival. Both updates can give the same result. Without an ORDER BY the roworder is not predictable. The rownum < 2 takes only the first row of the subselect.

掩饰不了的爱 2024-08-10 03:03:22

如果您使用的是 MS SQL Server 或 Sybase,您可以使用以下命令,

update foo set b = x from bar where bar.y = foo.b

抱歉,我没有看到您正在使用 Oracle。我想你必须为此创建存储过程。

If you are using MS SQL Server or Sybase, you can use following,

update foo set b = x from bar where bar.y = foo.b

Sorry, I did not see that you are using Oracle. I guess you would have to create Stored Procedure for that.

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