简体   繁体   English

php/mysql 将行加在一起以获得总数

[英]php/mysql add rows together to get total

here's the scenario.这是场景。 I am generating a report of all the members who have dues to pay for a certain time period.我正在生成一份报告,其中包含在特定时间段内需要支付会费的所有成员。

I am successfully selecting and displaying each database entry as a row in a html table.我成功地选择每个数据库条目并将其显示为 html 表中的一行。

The problem is the total fields the report must have.问题是报告必须具有的总字段。 Each member pays different amounts based on what services they use, so I must add the values in each field individually to ensure proper result.每个成员根据他们使用的服务支付不同的金额,因此我必须单独添加每个字段中的值以确保正确的结果。

Question is, how do I go about adding the rows/field together?问题是,我如何 go 关于将行/字段添加在一起?

Edit:编辑:

To clarify.澄清。 I am adding dues paid and donations paid fields.我正在添加已支付的会费和已支付的捐款字段。 They are classified and integer in mysql database.它们在 mysql 数据库中被分类为 integer。

Example, let's say that my query returns 3 results.例如,假设我的查询返回 3 个结果。 I wish to add the dues paid for all 3 results and display it as total_dues_paid.我希望添加为所有 3 个结果支付的会费并将其显示为 total_dues_paid。 Same idea for donations.捐赠的想法相同。

This must be dynamic for any given number of results, as this changes month to month and we have seen several hundred results in some months.对于任何给定数量的结果,这必须是动态的,因为这种情况每月都在变化,我们在几个月内已经看到了数百个结果。

Thanks谢谢

To add fields (columns):添加字段(列):

SELECT col1, col2, col3, (col1+col2+col3) AS Total FROM table;

To add rows together, use the SUM() aggregate:要将行添加在一起,请使用SUM()聚合:

SELECT
  userid,
  SUM(col1) AS col1_total,
  SUM(col2) AS col2_total
FROM table
GROUP BY userid

You can add in your query string.您可以添加查询字符串。

SELECT (field1 + field2) AS Total
FROM table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM