Incorrect usage of UNION and ORDER BY

A common problem when using the UNION command in MySQL is when you want to order the sets of data differently. If you do it incorrectly, you will see the error message:

Incorrect usage of UNION and ORDER BY

To solve the problem, wrap each individual SELECT statement in parenthesis as follows:

(SELECT col1,col2,col3 FROM table1)
UNION
(SELECT cola,colb,colc FROM table2)

Nice and simple, all you have to do is remember the brackets!

Are you a Developer, Designer or Copy Writer? Or Looking For One?

Click here to see 100s of jobs updated every day that you can complete to earn good money. Also find that programmer, designer or copy writer you have been looking for - advertise and get the best guy for your job - click here to find out more

6 Comments so far

  1. jimbo_dk on September 19th, 2007

    Thanks…I was having the same problem.

  2. [...] Die L?sung hast du ja nun schon selbst herausgefunden - Google spuckt dazu zB das hier aus: L?sungsvorschlag @ DevChunks.com [...]

  3. Anand Samyo on August 1st, 2008

    Thanks, i was looking for this

  4. arun on August 13th, 2009

    thanks for sharing … saved time for me

  5. Marco on January 4th, 2010

    Thank you also from me, I did appreciate the help!!

  6. M Hussain on April 22nd, 2010

    Thanks, It solves my problem.

Leave a Reply

Make $100,000/Month - Every Month!

It Works! We Make Our Customers Millionaires! Act now and get $600 bonus and a Free Money Making Website! - click to get started