Bug #28431 UNIOIN with ORDER BY returns error
Submitted: 15 May 2007 4:18 Modified: 15 May 2007 5:49
Reporter: Jared S (Silver Quality Contributor) Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Parser Severity:S3 (Non-critical)
Version:5.0.40 OS:Windows (Vista)
Assigned to: CPU Architecture:Any
Tags: order by, qc, query, UNION

[15 May 2007 4:18] Jared S
Description:
Hi, Have test case here that won't be parsed, it is a simple UNION query with ORDER BY.  The problem is that when ORDER BY exists on first half of union the query returns error.

How to repeat:
1. Upload my DB
2. Import my script to QB
[15 May 2007 5:49] Valeriy Kravchuk
Thank you for a problem report. Sorry, but this is not a bug. You have to use:

(select ... order by ...)
UNION
(select ... order by ...)

in cases like that. Please, read the manual, http://dev.mysql.com/doc/refman/5.0/en/union.html:

"To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one."

Same rule applies to your case also.