使用php从mysql db获取分类帐报告

时间:2013-01-25 15:46:38

标签: php mysql vba

嘿,我对php / mysql编程比较陌生。我正在将我们的数据库从Access迁移到mysql,我在vba中相对了解。

我的问题是我保留个人现金分类账的记录。事务存储在如下结构的数据库中:

tblclientpctransactions

transactionid
clientid -fk
date
description
depositamount
withdrawlamount

我需要做的是生成特定时间范围的分类帐,并按时间顺序显示信息并显示另一个字段,显示到那时为止的平衡。又名

  

日期| clientid |描述|存款金额withdrawlamount | runningbalance

在access / vba中我使用了dsum()函数来获取特定行的值,不知道如何实现这一点是php。

1 个答案:

答案 0 :(得分:0)

您可以在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

为简洁起见,将跳过所有错误处理和检查。