ORDER BY
suggest changeIf you need to sort the results of a UNION, use this pattern:
( SELECT ... )
UNION
( SELECT ... )
ORDER BY
A trailing ORDER BY sorts the whole UNION. Put parentheses around a branch when that branch has its own ORDER BY or LIMIT. Without them, MySQL rejects those clauses on the individual SELECTs.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents