SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    GET SUM WHERE in between...

    I'm trying to only get SUM of fields with an WHERE clause with between numbers...

    PHP Code:
    $sql1="SELECT SUM(*) as par_out FROM ".$prefix."_turn_data WHERE turnid=$sturn AND hole_no > 0 AND hole_no < 10"
    Something is wrong here, but cant figure out what?

    Any help apreciated...

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to specify the field name which you trying to get the SUM

    Code PHP:
    $sql1="SELECT SUM(FIELD_NAME) as par_out FROM ".$prefix."_turn_data WHERE turnid=$sturn AND hole_no > 0 AND hole_no < 10";

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Or use COUNT(*) if you're interested in the number of lines.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT SUM(*)
    The sum of what? Don't use *, which (under normal circumstances) means every single column in the table. You should be specifying the column that you actually want to sum, e.g.
    SELECT SUM(shots)
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lphy View Post
    You need to specify the field name which you trying to get the SUM

    Code PHP:
    $sql1="SELECT SUM(FIELD_NAME) as par_out FROM ".$prefix."_turn_data WHERE turnid=$sturn AND hole_no > 0 AND hole_no < 10";
    Ahhh, Thanks... Didn't see it. Thanks...

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,269
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jmansa View Post
    SELECT SUM(*) ...
    a very natural mistake to make, seeing as how COUNT(*) is so common

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •