I have two queries I would like to append in MySQL. I have found that UNION can help me do this, but it is not perfect as it does not retain the order of each appended query. In clear, I would like to do this:
(SELECT name,city FROM people WHERE xxx ORDER BY yyy)
UNION
(SELECT name,city FROM people WHERE zzz ORDER BY www)
but SQL won't execute on the ORDER statements when there is a UNION
One solution would be to add a bogus field to each subquery and order by that field first :
(SELECT name,city,'0' as bogus FROM people WHERE xxx)
UNION
(SELECT name,city,'1' as bogus FROM people WHERE zzz)
ORDER by bogus ASC, wwwzzz
but this is deeply inefficient as the query has to go through all fields to sort on the bogus field. Do you know of any workaround?
Thank you