PHP MYSQL SUM总计但显示行(PHP MYSQL SUM total but show rows)

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

所以第一页可能看起来像这样:

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页可能如下所示:

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

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

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 }

非常感激你的帮助! :)

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

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

But this is what I am getting:

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

Page 2 looks like this:

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

Try this 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

更多推荐