MySQL: Get total in last row of MySql result

(SELECT product, 
        quantity, 
        something 
 FROM   tablename) 
UNION 
(SELECT "all"          AS product, 
        SUM(quantity)  AS quantity, 
        SUM(something) AS something 
 FROM   tablename) 

This is working query. It will add a fourth row as desired at the end of your result


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

You could

SELECT Product, Quantity, Something 
FROM TABLENAME

UNION

SELECT 'ALL', SUM(Quantity),SUM(Something) 
FROM TABLENAME

This would not, however, add a row in your table. Which is probably not a good idea anyways.


You can use rollup to generate totals, but you have to change it to an aggregate function, like this:

SELECT product, sum(quantity), sum(something)
FROM tableName
GROUP BY product WITH ROLLUP

Tags:

Mysql

Sql

Php