How to sum the records of a given column

Asked

Viewed 24 times

0

Well, it’s as follows I have the table invoices, with a certain column designated by "earnings".

Each record, corresponds to a day, what I intend to do is a php script, which adds me all the "gains" of all the records, ie every day.

How can I do that?

Thank you

  • you can do by SQL... SUM(ganhos) as ganhos

  • I intend to do by PHP, to show me in the Adm panel.

  • 1

    In the link above you have examples of use of the general SUM, for certain condition and for grouping. If it is still not what you are looking for, you can [Dit] the question and add more details of your specific case to consider alternatives. For example, as he commented to do in PHP, it would be good to put the code snippet that shows the data, because if you have paging etc, changes everything.

  • 1

    Add the columns by SQL: SELECT SUM(ganhos) FROM tabela; and in PHP: $resultset = mysql_query('SELECT SUM(ganhos) as ganhos FROM tabela'); 
$linha= mysql_fetch_assoc($resultset); 
$soma = $linha['ganhos'];

  • 1

    And even in PHP if it’s not going to display all the data on the screen, SUM is still the most practical (it’s a select like any other, you can take PHP and show it on the screen) - SUM is just not an advantage when the entire listing will be displayed, then you just do a $total = $total + $row['valor'] inside the loop

  • I got it, thank you very much Marllon, that’s how I got it!

  • Thank you all for your help!

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.