Add an extra column to UNIONed queries and make this column the first column in the ORDER BY clause.
So, if I started with something like this:
SELECT product, price
FROM table
UNION
SELECT 'Total' AS product, SUM(price)
FROM table
I would add a new column:
SELECT product, price
FROM (
SELECT product, price, 0 AS union_order
FROM table
UNION
SELECT 'Total' AS product, SUM(price), 1 AS union_order
FROM table
)
ORDER BY union_order
Thus, ordinary products first appear, then the total number appears at the end.
source
share