SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question SELECTing * and adding total of a column to the result?

    I have a query that works fine, and I'd like to (if possible) in the same swipe add the total of the values of one column to the $result. In other words, I want to select * from the db and to the result, also add the sum of the values in a particular column. I have this:

    Code:
    $query = "SELECT * FROM $myTable $sort";
    ...where $sort is populated according to whatever link was clicked...it's either blank or DESC...and woot! it works...

    ...so the result is sorted, then list()ed into $result->fields... which I've then assigned to $stuff[] as such:

    Code:
    for (; !$result->EOF; $result->MoveNext())
    {
        list($this,$that,$etc) = $result->fields;
        $stuff[] = array('this'=> $this, 'that'=>$that, 'etc'=>$etc)
    }
    
    $result->Close();
    
    return $stuff;
    Is is possible to add up the values of $etc for all the rows returned and also include that value in the result? Or do I have to do another query separately to grab the sum of the values of a column? This would be the desired result:

    Code:
    $stuff[] = array(
    'this'=>$this,
    'that'=>$that,
    'etc'=>$etc,
    'TotalOfTheEtcColumn',$TotalOfTheEtcColumn
    );
    Hopefully this crazy idea is explained well enough... I think I'm going about it in the wrong way, but any assistance would be great!

    Thanks,

    Cranjled

  2. #2
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to say that you could do:
    PHP Code:
    $sql "SELECT *, SUM(column1, column2, ...) as sumvariable FROM yourtable"
    ...//do the query and get a row
    echo($row["sumvariable"]); 
    Might want to consider posting this in the Database/MySQL forums as this seems to be more of a database question than a PHP one.

  3. #3
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DOH! Sorry about that...wrong forum... 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
  •