-7

In some queries, for example UNION, ORDER BY clause doesn't seem to work so what to do in that situation? Is there any similar clause to ORDER BY?

Like there is a similar clause to "LIMIT x" is "SELECT TOP x" where x is the numeric number.

Adil
  • 39
  • 4

2 Answers2

3

If you want to order the individual selects in an union, you need parentheses and a LIMIT:

(SELECT a FROM b ORDER BY c LIMIT 10)
UNION
(SELECT d FROM e ORDER BY f LIMIT 10)

If you want to sort the union, again, use parentheses:

(SELECT a FROM b)
UNION
(SELECT d FROM e)
ORDER BY x

You can also read this in the manual.

Esailija
  • 138,174
  • 23
  • 272
  • 326
1

This post answers your question. You don't need a different clause to ORDER BY, just a different structure to your query.

Community
  • 1
  • 1
Steve Westbrook
  • 1,698
  • 2
  • 20
  • 21