你可以在 MySql 中做这样的事情来获取你的报告数据:
SELECT `date`,
`clientid`,
`description`,
`depositamount`,
`withdrawlamount`,
(@rt:=@rt + `depositamount` - `withdrawlamount`) AS runningbalance
FROM `transactions`, (SELECT @rt:=0) rt
WHERE `date` < CURDATE() AND
`clientid` = 1
ORDER BY `date`
CURDATE()
随您的报告日期更改。
现在,假设您的表中有以下数据:
transactionid clientid date description depositamount withdrawlamount
---------------------------------------------------------------------------
1 1 2013-01-01 NULL 100.00 0.00
2 1 2013-01-05 NULL 50.00 20.00
3 1 2013-01-07 NULL 0.00 30.00
4 1 2013-01-15 NULL 200.00 0.00
5 1 2013-01-20 NULL 0.00 50.00
这将是你的输出:
date clientid description depositamount withdrawlamount runningtotal
--------------------------------------------------------------------------
2013-01-01 1 NULL 100.00 0.00 100.00
2013-01-05 1 NULL 50.00 20.00 130.00
2013-01-07 1 NULL 0.00 30.00 100.00
2013-01-15 1 NULL 200.00 0.00 300.00
2013-01-20 1 NULL 0.00 50.00 250.00
然后,您可以像这样在 php 脚本中使用查询:
date_default_timezone_set('America/New_York');
$client_id = 1;
$report_date = date('Y-m-d');
$db = new PDO('mysql:host=localhost;dbname=yourdbname;charset=UTF-8', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$query = $db->prepare("
SELECT `date`,
`clientid`,
`description`,
`depositamount`,
`withdrawlamount`,
(@rt:=@rt + `depositamount` - `withdrawlamount`) AS runningbalance
FROM `transactions`, (SELECT @rt:=0) rt
WHERE `date` < :report_date AND
`clientid` = :client_id
ORDER BY `date`");
$query->execute(array(':report_date' => $report_date, ':client_id' => $client_id));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
//your code to render the report goes here
为简洁起见,将跳过所有错误处理和检查。