一个SQL语句,不难但是我一直调试通不过

iranjn 2007-04-12 08:27:54
A表
user mny
001 100
002 20

B表

user SumMNY
001 10
002 0
003 0

用一条update语句完成A表的数据更新B表的数据
完成结果如下

B表
user SumMNY
001 110
002 20
003 0
...全文
275 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
iranjn 2007-04-13
  • 打赏
  • 举报
回复
Eric_1999差点被你害死 update eric4 b set SumMNY=nvl(SumMNY+(select mny from eric3 a where a.users=b.users), 0)我会死的很参的
update eric4 b set SumMNY=SumMNY+nvl((select mny from eric3 a where a.users=b.users), 0)才对,到现在心还跳的厉害呢
DragonBill 2007-04-13
  • 打赏
  • 举报
回复
抱错:单行子查询返回多个行
可能是因为b中的user与a中的user是一对多关系

update b set summny = nvl(summny + (select nvl(sum(mny),0) from a where user=b.user group by user),summny)
mqmmx 2007-04-13
  • 打赏
  • 举报
回复
update b set SumMNY=SumMNY+(select nvl(sum(mny),0) from a where a.user=b.user group by user) where exists(select '' from b,a where a.user=b.user)
mqmmx 2007-04-13
  • 打赏
  • 举报
回复
update b set SumMNY=SumMNY+(select nvl(sum(mny),0) from a where a.user=b.user group by user)
Eric_1999 2007-04-13
  • 打赏
  • 举报
回复
SQL> set serveroutput on
SQL> select * from eric3
2 /

USERS MNY
---------- ----------
1 100
2 20

SQL> select * from eric4
2 /

USERS SUMMNY
---------- ----------
3 0
2 0
1 10

SQL> update eric4 b set SumMNY=nvl(SumMNY+(select mny from eric3 a where a.users=b.users), 0)
2 /

3 rows updated.

SQL> select * from eric4
2 /

USERS SUMMNY
---------- ----------
3 0
2 20
1 110

SQL>
iranjn 2007-04-12
  • 打赏
  • 举报
回复
楼上的 oracle可以这样写吗
userqin 2007-04-12
  • 打赏
  • 举报
回复
update 表b set SumMNY=a.SumMNY+b.SumMNY from 表a a,表b b where b.user=a.user
iranjn 2007-04-12
  • 打赏
  • 举报
回复
不行,抱错:单行子查询返回多个行!!
hongqi162 2007-04-12
  • 打赏
  • 举报
回复
update b set SumMNY=SumMNY+(select mny from a where a.user=b.user)

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧