一个SQL语句,不难但是我一直调试通不过
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
------解决方案--------------------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>
------解决方案--------------------update b set SumMNY=SumMNY+(select nvl(sum(mny),0) from a where a.user=b.user group by user)
------解决方案--------------------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)
------解决方案--------------------抱错:单行子查询返回多个行
可能是因为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)