レポート データを取得するには、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
簡潔にするために、すべてのエラー処理とチェックはスキップされます。