这条SQL怎样写(25分)

  • 主题发起人 devexpress
  • 开始时间
D

devexpress

Unregistered / Unconfirmed
GUEST, unregistred user!
hyk表

Password, yfmoney (应付)
1 0
2 0
3 0

xsb表
Password dj (单价) sl(数量)
1 1 5
1 1 10
2 2 10
2 1 10
3 1 10

怎样得到下样的结果
Password, yfmoney (应付)
1 15
2 30
3 10

也就是把xsb里 按
sum(dj*sl), group by password, where hyk.password=xsb.password
的条件汇总
把最后的金额加到hyk表里。

谢谢
 
update hyk
set hyk.yfmoney=hyk.yfmoney+D.yfmoney
From hyk left join
(Select Password,sum(dj*sl) as yfmoney from xsb group by Password) as D
on hyk.ghbh=D.password

自己解决。不知哪位有更好的算法没有。
 
update hyk a
set
a.yfmoney=a.yfmoney+(b.dj*b.sl)
from xsb b
where a.password=b.password
 
update hyk
set
yfmoney=b.sumx
from (select password,sum(dj*sl) as sumx from xsb group by password) b
where hyk.password=b.password
 
做视图不可以吗?
create view hykandxsb as
select a.password password,sum(b.dj*b.sl) yfmoney from hyk a,xsb b
where a.password = b.password
group by a.password
order by a.password;
问一下你用的什么数据库?
 
updata hyk
set yfmoney=(select sum(dj*sl) from xsk where hyk.password=xsk.password)
 
和楼上看法一样!
 

Similar threads

D
回复
0
查看
513
DelphiTeacher的专栏
D
D
回复
0
查看
524
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
顶部