开发者

php/mysql add rows together to get total

开发者 https://www.devze.com 2023-03-12 22:00 出处:网络
here\'s the scenario. I am generating a report of all the members who have dues to pay for a certain time period.

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.

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 fi开发者_Python百科eld individually to ensure proper result.

Question is, how do I go about adding the rows/field together?

Edit:

To clarify. I am adding dues paid and donations paid fields. They are classified and integer in mysql database.

Example, let's say that my query returns 3 results. I wish to add the dues paid for all 3 results and display it as 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:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号