Alex Rivera | Logout

MySQL: Get total in last row of MySql result

Asked 2011-03-08T12:15:38.717
13

For example I have a table like this:

product | quantity | something
-------------------------------
 abc    |   5      |  2
 xzy    |   5      |  2
 asd    |   10     |  2

When I need all record I do like this and get 3 results:

select * from tableName 

But I want get a 4th row for total of quantity(50) and something(6). Is it possible in sql query or I have to loop my result to get total after query execution? I want to do this in query if possible.

Edit
Report

2 Answers

3

it's not the best way to solve this but should do the trick

select product, quantity, something from tableName
union 
select 'sum', sum(quantity), sum(something) from tableName
answered 2011-03-08T12:20:44.720
1
$q="select sum(quantity), sum(something) from tableName";
answered 2011-03-08T12:19:52.400

Your Answer