MySQL SUM() question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

MySQL SUM() question

Post by Illusionist »

Hey i have a select statement like:

"SELECT *, SUM(calls) FROM tablename"

but... will it work? I mean i try using mysql_fetch_row and _fetch but always get errors... If this can be done this way, can someone show me how i would go about getting the value of SUM(calls)??
Thanks!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

$sql = "SELECT SUM(calls) AS total FROM tablename";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
echo 'Total : '.$row['total'];
You can't select *, SUM(calls) as you'de be trying to select all rows, and a SUM ... where would it put the SUM :o
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

lol, alright! thansk for clearing that up for me! I was started to thinkt aht i wasn't going to be able to do that. But ok! Thanks for the example!
Post Reply