当前页面: 开发资料首页 → JSP 专题 → 求一SQL语句(呵呵)
求一SQL语句(呵呵)
摘要: 求一SQL语句(呵呵)
现有两个表:入库材料表,出库材料表
入库:编码(bm),重量(zl) 表名:LK
001 100
002 200
003 200
出库:编码(bm),重量(zl) 表名: CK
001 -80
003 -110
现??
求一SQL语句,求出每种材料的库存,如按上面的数据应该为
001 20
002 200
003 90
数据库sql server
先谢谢各位了。。呵呵!!……——……
select lk.bm,lk.zl+ck.zl from lk,ck where lk.bm=ck.bm
select a.bm,sum(isnull(a.zl,0)+isnull(b.zl,0)) as zl from LK a,CK b where a.bm=b.bm union
(select b.bm,sum(isnull(a.zl,0)+isnull(b.zl,0)) as zl from LK a,CK b where a.bm=b.bm)
select x.bm,sum(x.zl)
from (select bm,zl from LK
union all
select bm,zl from CK) x
group by x.bm