4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 Sum of rows
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Sat Apr 26, 2003 3:33 pm (20 years, 12 months ago) Reply with QuoteBack to Top

Is there an easy way to add up the values of rows in a colum.
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
SfCommand
Senior WebHelper
Senior WebHelper


Joined: 10 Nov 2002
Posts: 143
Location: UK

PostPosted: Sat Apr 26, 2003 3:40 pm (20 years, 12 months ago) Reply with QuoteBack to Top

I use this:
Code:
SELECT SUM(FieldName) from TableName

________________________________
Miguel

http://community.34sp.com
http://www.miguel.me.uk | http://www.sfcommand.co.uk | http://www.ssdg.org.uk
OfflineView User's ProfileFind all posts by SfCommandSend Personal MessageVisit Poster's Website
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Sat Apr 26, 2003 8:32 pm (20 years, 12 months ago) Reply with QuoteBack to Top

Does this need to be queried, how do I put this into a script to display the sum.
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
SfCommand
Senior WebHelper
Senior WebHelper


Joined: 10 Nov 2002
Posts: 143
Location: UK

PostPosted: Sat Apr 26, 2003 9:03 pm (20 years, 12 months ago) Reply with QuoteBack to Top

I just use:
Code:
$total = mysql_query("SELECT SUM(some_field) from some_table");
echo $total

HTH

________________________________
Miguel

http://community.34sp.com
http://www.miguel.me.uk | http://www.sfcommand.co.uk | http://www.ssdg.org.uk
OfflineView User's ProfileFind all posts by SfCommandSend Personal MessageVisit Poster's Website
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Sat Apr 26, 2003 9:19 pm (20 years, 12 months ago) Reply with QuoteBack to Top

It echos Resource id #2 when I use this code, any ideas why?
Code:
$total = mysql_query("SELECT SUM(ab) from Robinson_Stats_2");
echo $total;

The field as 3 rows 100, 69, 122, so it should echo 291 right?
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Sat Apr 26, 2003 9:30 pm (20 years, 12 months ago) Reply with QuoteBack to Top

You have to fetch the results first Wink

Arrow http://www.php.net/manual/en/function.mysql-fetch-array.php

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Sat Apr 26, 2003 9:51 pm (20 years, 12 months ago) Reply with QuoteBack to Top

I dont get how to put this together, here is what I have and it echos Array
Code:
$total = mysql_query("SELECT SUM(ab) from Robinson_Stats_2");
$display = mysql_fetch_array($total);
echo $display;
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
SfCommand
Senior WebHelper
Senior WebHelper


Joined: 10 Nov 2002
Posts: 143
Location: UK

PostPosted: Sat Apr 26, 2003 11:46 pm (20 years, 12 months ago) Reply with QuoteBack to Top

OK. Here's what I have that does what you want:
Code:
mysql_connect (<dbhost>, <db_user>, <db_pass>);
mysql_select_DB(<db_name>);
$total = mysql_query("SELECT SUM(someField) FROM someTable");
echo "$total[0]";


ooops, looks like I forgot to put the [0] bit in last time (guess that'll teach me to check that in the advert break between progs on BBC1 Smile

[edit]
Oops, looks like I should double check what I post to make sure I'm posting the right thing Smile
[/edit]

________________________________
Miguel

http://community.34sp.com
http://www.miguel.me.uk | http://www.sfcommand.co.uk | http://www.ssdg.org.uk

Last edited by SfCommand on Sun Apr 27, 2003 4:28 pm, edited 1 time in total
OfflineView User's ProfileFind all posts by SfCommandSend Personal MessageVisit Poster's Website
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Sun Apr 27, 2003 2:25 am (20 years, 12 months ago) Reply with QuoteBack to Top

This is what I have and its not displaying anything.
Code:
$total = mysql_query("SELECT COUNT(ab) FROM Robinson_Stats_2");
echo "$total[0]";

I did connect to the database too.
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Sun Apr 27, 2003 2:33 am (20 years, 12 months ago) Reply with QuoteBack to Top

I really need to find this out, I am supposed to have the stats part done by tomorrow their first game. So if anyone knows how to get this to work please let me know.
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
drathbun
WebHelper
WebHelper


Joined: 01 Mar 2003
Posts: 69
Location: Texas

PostPosted: Sun Apr 27, 2003 5:19 am (20 years, 12 months ago) Reply with QuoteBack to Top

Assuming that you have already connected to the database...
Code:
        $sql = "SELECT count(ab) as at_bats
                FROM Robinson_Stats_2";

        if ( !($result = mysql_query($sql)) )
        {
           die('Could not obtain at bats information');
        }

        $row = mysql_fetch_assoc($result) ;
        $total = $row['at_bats'];

By using mysql_fetch_assoc() you get an associative array of column results. You should name any column results that are calculated, like your count(), so that you have a descriptive name to work with. Then you reference the results using the array, indexed by column name.

Dave

________________________________
Dave
Photography Site :: Query Tools Forum :: Weekend Fun
OfflineView User's ProfileFind all posts by drathbunSend Personal MessageVisit Poster's Website
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Sun Apr 27, 2003 4:15 pm (20 years, 12 months ago) Reply with QuoteBack to Top

This code tells me how many rows there are, im trying to add the content of the rows up.
Code:
$sql = "SELECT count(ab) as at_bats
       FROM Robinson_Stats_2";
if ( !($result = mysql_query($sql)) )
{
die('Could not obtain at bats information');
}
$row = mysql_fetch_assoc($result) ;
$total = $row['at_bats'];
echo "$total"
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Sun Apr 27, 2003 4:23 pm (20 years, 12 months ago) Reply with QuoteBack to Top

As someone said above, replace:
Code:
count(ab)
with
Code:
sum(ab)

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
drathbun
WebHelper
WebHelper


Joined: 01 Mar 2003
Posts: 69
Location: Texas

PostPosted: Sun Apr 27, 2003 8:36 pm (20 years, 11 months ago) Reply with QuoteBack to Top

Daniel wrote:
As someone said above, replace:
Code:
count(ab)
with
Code:
sum(ab)

I was going with the code that was just a couple of posts above mine. If each "at bat" was stored as a row, then you would count them. If the ab column contains a number, then you sum them. It wasn't clear from your description which way you needed to go.

But I'm assuming that the code is working since you said you are getting data back now. Cool At this point you have a template of sorts to get you going. Good luck!

Dave

________________________________
Dave
Photography Site :: Query Tools Forum :: Weekend Fun
OfflineView User's ProfileFind all posts by drathbunSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.128653 seconds :: 18 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme