PHP MYSQL总和,但秀行(PHP MYSQL SUM total but show rows)

2019-09-30 05:58发布

我所试图做的是显示在顶部的最后一个条目页面显示收入。 但它仍然需要获得总额高达每行。

因此,一个页面可能是这样的:

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
}

非常感激你的帮助! :)

Answer 1:

试试这个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 total but show rows
标签: php mysql sum