使用另一列中的值更新列
我有一个像这样的表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试将 foo.a 更新为 NULL 有两个可能的原因。
如果上述任一条件为真,则以下内容将排除对 foo 的更新。在这些情况下 foo.a 将保持原样:
There are two possible reasons for the update to attempting to foo.a to 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:
这会失败/旋转:
...因为您正在更新您想要用来确定更新内容的相同值。 Oracle 始终允许用户读取数据。
This fails/spins:
...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.
可能会因性能原因而挂起,但应该可以工作。仔细检查如果没有 bar.y 等于 foo.b 会发生什么。如果将b设置为null可以吗?
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?
在您提供的查询中,您似乎有一个拼写错误。在过程代码中,您修改了 foo.a 的值,但查询更新了 foo.b:
此外,如果有许多行具有相同的 bar.y 值,则可能会出现问题。您的子查询可能返回一个结果集,而不是您的赋值所期望的单个值。
例如,如果您的数据是
那么“
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:
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
Then "
DISTINCT x
" will return '{1,2,3}
'假设您有以下值。
上面给出的答案引发了 ORA-01427 错误。该语句需要一些补充,这取决于预期的结果。
如果您期望最大的 bar(x,2) 应该存储在 foo(a,2) 中。
如果您期望 bar(x,2) 为任何值,请编写以下内容。
子选择的顺序取决于存储和行检索。两次更新可以给出相同的结果。如果没有 ORDER BY,则行顺序是不可预测的。 rownum 2 仅获取子选择的第一行。
Assume you have following values.
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).
If you expect any value of bar(x,2) than write following.
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.
如果您使用的是 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.