SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Apr 28, 2005, 12:28 #1
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";
...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;
Code:$stuff[] = array( 'this'=>$this, 'that'=>$that, 'etc'=>$etc, 'TotalOfTheEtcColumn',$TotalOfTheEtcColumn );
Thanks,
Cranjled
-
Apr 28, 2005, 13:36 #2
- 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"]);
-
Apr 28, 2005, 13:55 #3
DOH! Sorry about that...wrong forum... Thanks.
Bookmarks