SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple queries for sum and select

    Is it better to use multiple mysql queries for sum and select?

    That is, which is better:

    mysql_query("select * from table",$connection);
    mysql_query("select sum(num) from table",$connection);

    ~or~

    mysql_query("select sum(num),* from table",$connection);

  2. #2
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jackli View Post
    Is it better to use multiple mysql queries for sum and select?

    That is, which is better:

    mysql_query("select * from table",$connection);
    mysql_query("select sum(num) from table",$connection);

    ~or~

    mysql_query("select sum(num),* from table",$connection);
    mysql_query("select sum(num) from table",$connection); query is wrong.If you are finding sum then you muct be finding of some field.I mean sum of sales or so

    then your query will
    select sum($num)
    from table
    group by 'fieldname'

    Exactly i ave no idea what u wants to achieve?Hope it can spark the idea in your mind how to go ahead.

    Thanks

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming the sum(num) num is the field name the both of the queries are fine and works. But i do like this:
    PHP Code:
    $res mysql_query("select sum(amount) as Amount from mytable");
    echo 
    mysql_result($res0"Amount"); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by priti View Post
    mysql_query("select sum(num) from table",$connection); query is wrong.If you are finding sum then you muct be finding of some field.I mean sum of sales or so
    Yes, and in this case jackli is finding a sum of field num. What's wrong with this?

  5. #5
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, let me clarify the question...

    suppose i have a table called "table" with columns like: "name" and "num" and a several other columns

    suppose i want to select the sum of all "num" rows from "table" AND i want to fetch all data from "table"

    is it better to do it all at once or to do separate queries?

    (or should i use php to sum "num" by just select * from table?)

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,015
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jackli View Post
    suppose i want to select the sum of all "num" rows from "table" AND i want to fetch all data from "table"
    in that case, get the entire table with one query, and do the sum with php while you're printing the rows out
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jackli View Post
    hi, let me clarify the question...

    suppose i have a table called "table" with columns like: "name" and "num" and a several other columns

    suppose i want to select the sum of all "num" rows from "table" AND i want to fetch all data from "table"

    is it better to do it all at once or to do separate queries?

    (or should i use php to sum "num" by just select * from table?)
    Oh i did not read carefully. You dont have to run two queries but you can do total while looping like this:
    PHP Code:
    $sql "SELECT name,num FROM tblname";
    $res mysql_query($sql);
    $total 0;
    while(
    $rows mysql_fetch_array($res)){
        echo 
    $rows['name'] . " " $rows['num'];
        
    $total += $rows['num'];
    }
    echo 
    "Total is : " $total
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Zealot smadeira's Avatar
    Join Date
    Oct 2003
    Location
    Pennsylvania
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe MySQL is different than SQL Server but trying to get a sum of a bunch of rows and getting the rows at the same time won't work. In order to do a sum of something you need to group together the things you are summing. I don't have access to a MySQL server here at work but I know doing both won't work in SQL Server which (sadly) is what we use here at work.
    Scott


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
  •