Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply "ORDER BY" on a "UNION" (Mysql)

So, everything is in the title.

I am looking to merge the result of two requests and order the result together (as in, not one after the other). => I was thinking of applying a union and ordering them. It didn't work.

I looked around like here on Stack or here developpez (!!french website). I've tried the different examples and suggestions, but no success. It seems from what I read that it's because I am working on Mysql.

Anyway, here are my attempts, and the results.

My original 2 requests:

SELECT * FROM user_relation WHERE from_user_id = 1
List item
SELECT * FROM user_relation WHERE to_user_id = 1

This result of a list made of the result of the first select (ordered by index key) followed by the result of 2nd select ordered by index key.

Attempt 1:

(SELECT * FROM user_relation WHERE from_user_id = 1 ORDER BY trust_degree)
UNION
(SELECT * FROM user_relation WHERE to_user_id = 1 ORDER BY trust_degree)

The request ran, but the result was the same as the original request: result of first select (ordered by index key) followed by the results of the second request.

Attempt 2:

(SELECT * FROM user_relation WHERE from_user_id = 1 ORDER BY trust_degree)
UNION
(SELECT * FROM user_relation WHERE to_user_id = 1 ORDER BY trust_degree)
ORDER BY trust_degree

Request ran, result same as attempt 1, but with a warning in the Mysql logic:
(this type of close has been already analysed (ORDER BY))

Attempt 3:

(SELECT * FROM user_relation WHERE from_user_id = 1
UNION
SELECT * FROM user_relation WHERE to_user_id = 1)
ORDER BY trust_degree

Did not run, but an error #1064 - syntax error near UNION.

Attempt 4:

SELECT *
FROM (
(SELECT * FROM user_relation WHERE from_user_id = 1)
UNION
(SELECT * FROM user_relation WHERE to_user_id = 1)
)
ORDER BY trust_degree 

Did not run, and a nice list of 6 errors. Any suggestions?

like image 475
Quiche Avatar asked May 02 '17 15:05

Quiche


People also ask

Can we use ORDER BY in UNION?

Union is a type of operator in MySQL. We can use ORDER BY with this to filter records. Use UNION if you want to select rows one after the other from several tables or several sets of rows from a single table all as a single result set. Let us see an example.

Why ORDER BY is not working with UNION?

UNIONS cause orders to be lost as it does a match up. You can fake it by adding a bogus limit to the union, then order the whole result.


1 Answers

SELECT *
FROM (
(SELECT * FROM user_relation WHERE from_user_id = 1)
UNION
(SELECT * FROM user_relation WHERE to_user_id = 1)
) AS i
ORDER BY trust_degree

You have to assign an alias to your select. But in this case a UNION is not necessary and could be replaced by a simple OR, as @Karoly Horvath points out in his comment. The resulting query would look like this:

SELECT 
 * 
FROM user_relation 
WHERE from_user_id = 1 OR to_user_id = 1 
ORDER BY trust_degree
like image 85
Michael Kunst Avatar answered Oct 31 '22 16:10

Michael Kunst