For example I have a table like this:
product | quantity | something
-------------------------------
abc | 5 | 2
xzy | 5 | 2
asd
$q="select sum(quantity), sum(something) from tableName";
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
Try to avoid union query it may increase the execution time in case of joins and case queries. So as @Chris Strickland said, use WITH ROLLUP.
(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
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
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.