Mail Beyond Logo
Posts
Comments

I’ve you’re just starting to play with a Union, then you might have gotten the error message:

#1221 – Incorrect usage of UNION and ORDER BY

You might be thinking: Whoa, hang on there! Do you mean MySQL can’t order a Union?! Oh, but it can! First little issue I had was in realizing that you can put parenthesis around the queries. So phrase your query thusly:

“(SELECT …) UNION (SELECT …) ORDER BY …”

Just be sure that your Order By includes only the column name. Table.Column is going to get you another error!

If you found this helpful, help me by checking out the ads on the right. Thank you!


Link to this post! Copy and paste this code into your blog or website:
<a href="http://www.mailbeyond.com/order-by-with-unions-in-mysql"> Order By with Union’s in MySQL </a>

3 Responses to “Order By with Union’s in MySQL”

  1. on 30 Jun 2010 at 6:19 am BobJoe

    Thank you!

  2. on 13 Jul 2010 at 8:22 am Chris

    Thanks for the tip. Very useful post.

  3. on 11 Aug 2010 at 6:34 am Jaymze Gazov

    Very clear and accurate description – and – it works !!! Thanks !!!

Trackback URI | Comments RSS

Leave a Reply