且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 PHP 中显示 MySQL 列的总数

更新时间:2023-01-29 16:28:17

这可以像这样在查询中完成.

this can be done in the query like so.

SELECT id, client, package, rate, SUM(rate) as total, term_start, term_end, last_billed 
FROM clients
group by id with Rollup

最后一行的 id 将为 null 和您想要的总数.

the last row will have an id of null and the total you want.

https://dev.mysql.com/doc/refman/5.6/en/group-by-modifiers.html

这不是 ROLLUP 最常见的用法,当您已经在进行聚合时,它更常用作超级聚合,例如按月分组销售并获得总计.但是,如果您需要数据来自查询而不是在外部计算,则对 id 进行分组将使其就像一个简单的总和.它在排序和限制方面确实有缺点.

This is not be the most common use of ROLLUP, its more often used as an super aggrigate when you are already doing an aggrigate, say group sales by month and get the grand total. However if you need the data to come from the query rather than being calculated outside, grouping on id will make it just like a simple sum. It does have downsides regarding sorting and limit.