我想做的是通过在顶部显示最后条目的页面显示收入。 但是它仍然需要得到每一行的总数。
所以第一页可能是这样的:
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
}
非常感谢您的帮助! :)
最佳答案
试试这个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 总计但显示行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16173444/