Page 1 of 1
Add Contents of Column
Posted: Mon Sep 19, 2005 12:26 pm
by Skara
Err.. I think I've done this before, but I don't remember how, and I can't find anywhere that says how.
Say you have a table as so...
Code: Select all
| 2 | 3 | 4 |
| 4 | 6 | 9 |
| 1 | 6 | 3 |
...
How would you add up a row in the query? e.g.
SELECT sum_of_contents(row2) AS blah;
Posted: Mon Sep 19, 2005 12:33 pm
by feyd
you'd have to add each field together... I'd prefer to (if I had to use a single row for this) do the math in PHP.. or rebuild the table such that each record is a single "column" value and use it's aggregate functions..
Posted: Mon Sep 19, 2005 12:52 pm
by yakasha
SELECT (col1 + col2 + col3) AS sum_of_cols FROM wherever;
Posted: Mon Sep 19, 2005 2:11 pm
by Skara
dammit.
I meant to say add up the rows in a column. -_-'
As in...
| 2 | 3 | 4 |
| 4 | 6 | 9 |
| 1 | 6 | 3 |
...
Where it might add 3+6+6 or 2+4+1.
Columns and rows... dammit.

Posted: Mon Sep 19, 2005 2:25 pm
by ryanlwh
SELECT SUM(col1) AS col1_total, SUM(col2) AS col2_total, SUM(col3) AS col3_total FROM whatever
Posted: Tue Sep 20, 2005 3:25 pm
by Skara
voila! Thanks! ^^;