Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ORDER BY inside UNION

Tags:

sql

mysql

union

I want to use ORDER BY on every UNION ALL queries, but I can't figure out the right syntax. This is what I want:

(
SELECT id, user_id, other_id, name 
FROM tablename 
WHERE user_id = 123 AND user_in IN (...) 
ORDER BY name
)
UNION ALL
(
SELECT id, user_id, other_id, name 
FROM tablename 
WHERE user_id = 456 AND user_id NOT IN (...) 
ORDER BY name
)

EDIT: Just to be clear: I need two ordered lists like this, not one:

1 2 3 1 2 3 4 5

Thank you very much!

like image 592
VORiAND Avatar asked Jul 10 '14 18:07

VORiAND


People also ask

Can you use ORDER BY in a union?

The columns in the ORDER BY list must be a subset of the columns in the select list of the left side of the union. All the columns in the ORDER BY list must be sorted in ascending order and they must be an in-order prefix of the columns in the target list of the left side of the UNION.

Why does ORDER BY not work with union?

Because of the parentheses, it is clear that the ORDER BY belongs to the result of the UNION , not the final SELECT . It is usually 'wrong' to have multiple tables with the same schema. Consider combining those tables into one, perhaps with an extra column to say which it is. Then you can get rid of the UNIONs .

Can we use ORDER BY clause in union in Oracle?

The Oracle UNION ALL operator can use the Oracle ORDER BY clause to order the results of the query.

Can we use ORDER BY in union in db2?

ORDER BY is not allowed in a view. A name may be specified on the ORDER BY clause if the result columns are named. A result column is named if the corresponding columns in each of the unioned select-statements have the same name. An AS clause can be used to assign a name to columns in the select list.


1 Answers

Don't use ORDER BY in an individual SELECT statement inside a UNION, unless you're using LIMIT with it.

The MySQL docs on UNION explain why (emphasis mine):

To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:

(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10) UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);

However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.

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. The following example uses both clauses:

(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;

It seems like an ORDER BY clause like the following will get you what you want:

ORDER BY user_id, name
like image 97
Marcus Adams Avatar answered Sep 19 '22 21:09

Marcus Adams