php mysql 统计进出总流量库存 [英] Count total flow inventory in and out php mysql

查看:64
本文介绍了php mysql 统计进出总流量库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了记录我没有使用不同的表,我使用同一个表进行计算,但我添加了更多名为 stock 的列.

我有一个记录表:

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|

我想要使用 select mysql 的输出是这样的:

The output that I want using select mysql is like this:

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

如您所见,我在表 A 中添加了股票列.. 所以我的问题是如何使用 mysql 实现这一点?

As you can see I added the stock column in table A.. so my question is how can I achieved that using mysql ?

更新

我被@Juergen D answer救了,所以我正在使用他的方法:

I've been saved by @Juergen D answer so I'm using his method:

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

如果你有和我一样的问题:)

in case you have a same problem as me :)

推荐答案

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

这篇关于php mysql 统计进出总流量库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆