Jump to content


Photo

How do I add up database entries


  • Please log in to reply
3 replies to this topic

#1 Noctagon

Noctagon
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 03 October 2006 - 02:36 AM

I am trying to run a query that will look at a specific table and then tally up the values in a certain field.

Cant figure out how to do this.

Any pointers would be greatly appreciated.

Thanks :)

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 October 2006 - 03:18 AM

use the SUM() function:
SELECT SUM(colName) FROM tableName;

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 03 October 2006 - 03:35 AM

Another way which you may be more comfortable with, though it has far worse performance is to loop through the result set and add them with php.

#4 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 03:59 AM

HUmmm Obsidian good call with mysql functions, I swear I always forget about mysql and how powerful it can be. Here is a php solution.

$sql = "SELECT myField FROM myDatabase WHERE myField = myClause ORDER BY myOrderBY";
$result = mysql_query($sql) OR die("Boom!");
while($row = mysql_fetch_array($result))
{
  $total += $row[0];
}




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users