数据库问题。一个购进数量表,一个销售数量表。如何即时动态得到库存数字。sql语句该怎么写?谢谢
select a.shl-b.shl as 库存数量 from
(SELECT SUM(shl),spid from ywsys_ywmx where djbh like 'JHD%' and zzrq>='2015-01-01' group by spid) a
(select SUM(shl),spid from ls_daymxls where rq>='2015-01-01' group by spid)b where a.spid=b.spid
报错 不知道怎么回事
报什么错?上表结构和测试数据。
(SELECT SUM(shl),spid from ywsys_ywmx where djbh like 'JHD%' and zzrq>='2015-01-01' group by spid) a ,
(select SUM(shl),spid from ls_daymxls where rq>='2015-01-01' group by spid)b where a.spid=b.spid
少个逗号。
或者这么写,结构清晰点。
with a as(
SELECT SUM(shl),spid from ywsys_ywmx where djbh like 'JHD%' and zzrq>='2015-01-01' group by spid
),
b as(
select SUM(shl),spid from ls_daymxls where rq>='2015-01-01' group by spid
)
select * from a , b where a.fzjid = b.fzjid
改成下面这样SUM(SHL)增加别名,改成SUM(shl) shl,如下:
select a.shl-b.shl as 库存数量 from
(SELECT SUM(shl) shl,spid from ywsys_ywmx where djbh like 'JHD%' and zzrq>='2015-01-01' group by spid) a
(select SUM(shl) shl,spid from ls_daymxls where rq>='2015-01-01' group by spid)b where a.spid=b.spid
真的是少了一个逗号哦,就在别名a 那里
真的是少了一个逗号哦,就在别名a 那里
别名a后少个逗号,增加两个别名SUM(shl) shl,另,这个sql语句貌似只得到库存变动值,而不是实际库存值
就A那里少个逗号,感觉逻辑是正确的啊。
具体报的什么错误?贴出来。
from a, b
ab之间要加逗号
既然是进销存 就必须有个库存表用于存放商品库存结存数量; 只有通过结存数量 购进数量 销售数量才能得到实时库存;