Incorrect usage of UNION and ORDER BY

A common problem using the UNION command in MySQL is when you want to order data sets 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 SELECT statement in parenthesis as follows:

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

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


7 Responses

Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,


If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the