PHP MYSQL SUM总计但显示行 [英] PHP MYSQL SUM total but show rows

查看:62
本文介绍了PHP MYSQL SUM总计但显示行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是通过在顶部显示最后一个条目的页面来显示收入.但它仍然需要得到每行的总数.

What I am trying to do is display earning by the page showing the last entries on top. But it still needs to get the total up to each row.

所以第一页可能是这样的:

So page one might look like this:

Date      Amount    Balance
1/9/2013   10.00    80.00
1/7/2013   10.00    70.00
1/6/2013   10.00    60.00

第 2 页可能如下所示:

Page 2 might look like this:

Date      Amount    Balance
1/5/2013   10.00    50.00
1/4/2013   10.00    40.00
1/3/2013   10.00    30.00
1/2/2013   10.00    20.00
1/1/2013   10.00    10.00

但这就是我得到的:

Date      Amount    Balance
1/9/2013   10.00    60.00
1/7/2013   10.00    70.00
1/6/2013   10.00    80.00

第 2 页如下所示:

Date      Amount    Balance
1/5/2013   10.00    10.00
1/4/2013   10.00    20.00
1/3/2013   10.00    30.00
1/2/2013   10.00    40.00
1/1/2013   10.00    50.00

请注意余额是向后的.但即使我的示例没有显示,金额的顺序也是正确的.这是我的代码:

Notice that the balances are backward. But even though my example doesn't show it, the amounts are in the right order. Here is my code:

SELECT *,
@total:= @total+ `companyearned` AS `total`
FROM `recordedhours`, (SELECT @total:=0) r WHERE `group` = '$uid'
ORDER BY `unixdate` DESC, `idnum` DESC
LIMIT $from, $max_results

 while ($rowb = mysql_fetch_array($result2)) {
//CREATE ROWS HERE
}

非常感谢您的帮助!:)

Your help is very much appreciated! :)

推荐答案

试试这个 SQL

SELECT * FROM
(
    SELECT *, @total:= @total+ `companyearned` AS `total`
    FROM `recordedhours`, (SELECT @total:=0) r WHERE `group` = '$uid'
    ORDER BY `unixdate` ASC, `idnum` DESC
    LIMIT $from, $max_results
) tab ORDER BY `unixdate` DESC

这篇关于PHP MYSQL SUM总计但显示行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆