本文主要是介绍mysql 库存表 累计 sql语句 第一方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这是一个表,要求累计金额 。表名t ,字段lsh,shangpinbh,jine。
流水号 | 商品编号 | 金额 | 累计金额 |
1 | 1001 | 2500 | 2500 |
2 | 1001 | 3000 | 5500 |
3 | 1004 | 4000 | 9500 |
4 | 1006 | 1000 | 10500 |
5 | 1007 | 2000 | 12500 |
select lsh,shangpinbh,jine,(select sum(jine) from t where shangpinbh=t1.shangpinbh and lsh <=t1.lsh) as 累计金额from t as t1;
mariadb> select lsh,shangpinbh,jine,
(select sum(jine) from t where shangpinbh=t1.shangpinbh and lsh <=t1.lsh) as 累计金额
from t as t1;
+-----+------------+-------+----------+
| lsh | shangpinbh | jine | 累计金额 |
+-----+------------+-------+----------+
| 0 | 1002 | 5100 | 5100 |
| 1 | 1002 | 3500 | 8600 |
| 3 | 1002 | 10000 | 18600 |
| 4 | 2001 | 2560 | 2560 |
| 5 | 2010 | 4700 | 4700 |
+-----+------------+-------+----------+
5 rows in set (0.01 sec)
这篇关于mysql 库存表 累计 sql语句 第一方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!