我正在处理一个简单的流程语句,其中用户输入+ve
和-ve
值,它们存储在单个表中。
表结构是
funds_T (date, type, amount)
当我运行select all查询时,它将返回
Date | Type | Amount
2011-10-01 opening 26000.00
2011-11-20 payment -2000.00
2011-10-10 receipt 6000.00
2011-10-09 expense -8000.00
我想要的是旁边的另一栏显示累计总数,如下所示。
Date | Type | Amount | Total
2011-10-01 opening 26000.00 26000.00
2011-11-20 payment -2000.00 24000.00
2011-10-10 receipt 6000.00 30000.00
2011-10-09 expense -8000.00 22000.00
另外,有没有办法可以像在Excel中那样进行条件格式化,如果值>东西(背景颜色是红色等)
我当前的查询是
SELECT date,type,amount FROM funds_T WHERE date >= '$fromdate' AND date <= '$todate'
以下是我在Pentium的建议之后使用的查询
mysql_query(" SET @total: =0;
SELECT date,type,comments,amount,@total := @total + amount AS total
FROM funds_T
WHERE
date BETWEEN '$frdate' AND '$todate'
ORDER BY date ASC ");
答案 0 :(得分:0)
PHP中可以使用颜色和格式,而累积总数可以在SQL或PHP中完成。我会在for循环中重新计算运行时PHP中的总数。
如果您仍想使用SQL,则需要使用User defined variables
这将是:
SET @total:=0;
SELECT `date`,
`type`,
`amount`,
@total := @total + `amount` AS `total`
FROM funds_t
WHERE `date` BETWEEN '$fromdate' AND '$todate'
ORDER BY `date` ASC