Adding up records in MySQL

Discussion in 'PHP' started by MajHate, Nov 12, 2008.

  1. #1
    Hello,

    Let's say I have a table with the fields ID, NAME and RANK. Let's say we have:

    ID | NAME | RANK
    ------------------
    1 | John | 8
    2 | Jack | 7
    3 | Jack | 5
    4 | John | 6


    I want to add up all the values of RANK for Jack only, how would this be done?

    Thank You,
    Jason
     
    MajHate, Nov 12, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SELECT SUM(rank) AS rank_sum FROM tableName WHERE name='Jack';

    should work i think..
     
    rene7705, Nov 12, 2008 IP
  3. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    
    SELECT SUM(rank) FROM table WHERE name='Jack'
    --the field 'rank' should be an INTEGER.
    
    Code (SQL):
     
    rohan_shenoy, Nov 12, 2008 IP
  4. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Hmm. This does not seem to be working all I get when I echo it is "Resource id #9" Does anybody know whats going on?
     
    MajHate, Nov 12, 2008 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,933
    Likes Received:
    4,563
    Best Answers:
    124
    Trophy Points:
    665
    #5
    That's because you're not referencing the result properly.
     
    sarahk, Nov 12, 2008 IP
  6. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    Well how do i reference them properly?
     
    MajHate, Nov 12, 2008 IP
  7. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    with what php command did you execute the query?
     
    rene7705, Nov 12, 2008 IP
  8. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    Here is what I have. then I go echo $sum; and it displays: Resource id #9

    $sum = mysql_query("SELECT SUM(rank) FROM rank WHERE PID='$PID'") or die(mysql_error());
    Code (markup):
     
    MajHate, Nov 12, 2008 IP
  9. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    rene7705, Nov 12, 2008 IP
  10. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #10
    All that does is run the query, you need to process the result:

    
    $sum = mysql_query("SELECT SUM(rank) FROM rank WHERE PID='$PID'") or die(mysql_error());
    $sumrank = 0;
    while($sumrow = mysql_fetch_object($sum)) {
      $sumrank = $sumrow->rank;
    }
    
    PHP:
     
    Christian Little, Nov 12, 2008 IP
  11. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    I am still having trouble, what variable can I echo with? Also does it matter than the field and the table are both called rank?
     
    MajHate, Nov 12, 2008 IP
  12. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It shouldn't, but try this code:

    
    $sum = mysql_query("SELECT SUM(rank) as Total FROM rank WHERE PID='$PID'") or die(mysql_error());
    $sumrank = 0;
    while($sumrow = mysql_fetch_object($sum)) {
      $sumrank = $sumrow->total;
    }
    echo "Rank: $sumrank";
    
    PHP:
     
    Christian Little, Nov 12, 2008 IP
    sarahk likes this.