繁体   English   中英

总库存库存mysql

[英]total inventory stock mysql

如果您不介意,我会对我的这个问题有一个后续问题。

计算进出PHP MySQL的总流量清单

Table A
=======================================================
**id** | **code** | **status** | **total** | **date** |
1      | B01      | IN         | 500       |2013-01-15|
2      | B01      | OUT        | 100       |2013-01-20|
3      | B01      | OUT        | 200       |2013-02-01|
4      | B01      | IN         | 300       |2013-02-05|

从上表中我想要这样的输出..

Table A
===================================================================================
**id** | **code** | **status** | **total** | **date** | **stock** | 1st month stock
1      | B01      | IN         | 500       |2013-01-15| 500       | -
2      | B01      | OUT        | 100       |2013-01-20| 400       | 500
3      | B01      | IN         | 200       |2013-02-01| 600       | 400
4      | B01      | OUT        | 300       |2013-02-05| 300       | 600
5      |          |            |           |          |           | 300

我如何使用mysql实现呢? 有办法吗?

对于股票列,我已经使用这种方法实现了

select t.*, @stock := @stock + case when status = 'IN' 
                                    then total
                                    else -total  
                               end as stock
from your_table t
cross join (select @stock := 0) s
order by t.id

上一个问题开始 ,请帮助我,我真的是mysql新手

根据已有的内容,可以向SELECT列表中添加另一个表达式,

select t.*, @stock AS `1st month stock`, @stock := ...

注意:在将新值分配给SELECT列表中的@stock之前,需要返回@stock的当前值,因此列的顺序与所需输出中所示的顺序略有不同。


根据当前查询,只需将新表达式添加到SELECT列表中,即可返回另一列,例如

select t.*
     , @stock AS `1st month stock`
     , @stock := @stock + case when status = 'IN' 
                                then total
                                else -total  
                           end as stock
from your_table t
cross join (select @stock := 0) s
order by t.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM