SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Modesto, CA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post SUM () with multiple colums

    Hello Again, I need to add up the totals from 3 different colums I was wondering if I have to print this out for each one? or if I can do it in one shot?

    This is what I need to do.
    PHP Code:
    SELECT SUM(kills) AS mod_kills from item WHERE idnub 3040 OR idnub 3041

    SELECT SUM
    (deaths) AS mod_deaths from item WHERE idnub 3040 OR idnub 3041

    SELECT SUM
    (shots) AS mod_shots from item WHERE idnub 3040 OR idnub 3041 
    This is how Im performing it for now.. unless you guys can help me do it better.
    PHP Code:
    <?php
        
    // Requesting a Search for User Info
        
    $result = @mysql_query("SELECT SUM(kills) AS mod_kills from item WHERE idnub = 3040 OR idnub = 3041");
        if (!
    $result) {
            echo(
    "<p>Error performing query: " mysql_error() . "</p>");
        exit();
        }
        
        
    // Displays the results 
        
    while ( $row mysql_fetch_array($result) ) {
         
    $mod_kills = ($row["mod_kills"]);
        }
    ?>

    <? echo ($mod_kills); ?>

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think this will work:

    Code:
    SELECT SUM(kills) AS mod_kills, SUM(deaths) AS mod_deaths, SUM(shots) AS mod_shots
    	FROM item WHERE idnub IN(3040, 3041)
    i just used IN instead of the OR b/c i think it looks cleaner. but you can use OR if you want. it'll work the same.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Modesto, CA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, That worked Great and I like the way yours looks so I will swith to IN().

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •