19

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?

7
  • 4
    how about a simple OR ? :) Commented May 2, 2017 at 15:43
  • Have you tried selecting your unioned results INTO a temp table and then SELECT and ORDER BY from there?
    – nageeb
    Commented May 2, 2017 at 15:46
  • I am so blond... thx!! :D
    – Quiche
    Commented May 2, 2017 at 15:58
  • @KarolyHorvath, the OR does no work... cause if I have a user in both column, i don't seem to be able to retrive resutlts without him.
    – Quiche
    Commented May 2, 2017 at 16:04
  • @Quiche: it will work. Honestly, I have no idea what you're talking about. Commented May 2, 2017 at 16:42

4 Answers 4

26
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
3
  • Thanks, it worked.i did miss the "AS" (still learning :)) and... no, OR does not do it. (I did try it first, even tho i am learning ;))) if a user is present on both side of the relation, he is counted twice. The union allow me to avoid manipulating the results once i get them.
    – Quiche
    Commented May 2, 2017 at 16:09
  • Can you tell me what you want to do exactly? What happens with the data you selected after? Commented May 3, 2017 at 9:49
  • well, my initial idea at that time was to not do a OR so the ID for which i do the sleect doesn't appear on my result to ease my php afterward. But once i get the solution above and applied it, i had to left it. cause for eom reason i can 't explain myself, once reading teh answer o both slect combien together ... it actually mergeed the result of the two columns in 1 (to_user_id)... even the one from "from_user_id". so... not knowing how the change f column was based on,. and i fixed it by forcing the smaller id of a line pair in the right column. like (1|2) (3|9) using or!
    – Quiche
    Commented May 4, 2017 at 19:46
16

It is written in the documentation of UNION:

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);

...

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.

...

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;

A statement without parentheses is equivalent to one parenthesized as just shown.

By applying the above information to your query it becomes:

(SELECT * FROM user_relation WHERE from_user_id = 1)
UNION
(SELECT * FROM user_relation WHERE to_user_id = 1)
ORDER BY trust_degree
1
  • You got my token too...Trying all teh combo, i missed this one, I was sure i tried (To be honnest, i just took a another look at my post as i was sure to have used that solution. Thx mate
    – Quiche
    Commented May 2, 2017 at 16:57
0
SELECT a FROM t1 WHERE a=10 AND B=1
UNION
SELECT a FROM t2 WHERE a=11 AND B=2
ORDER BY 1 LIMIT 10;

if you need sort by more fields just add the number of the field, ORDER BY 1, 2, 3 LIMIT 10;

1
-2

To expand on my comment:

SELECT * into #tempTable FROM (
   SELECT * FROM user_relation WHERE from_user_id = 1
   UNION
   SELECT * FROM user_relation WHERE to_user_id = 1
)
as x

SELECT * FROM #tempTable ORDER BY trust_degree
DROP TABLE #temptable
2
  • 1
    I would strongly advise against creating temporary tables for simple selection queries whenever possible. Commented Feb 11, 2019 at 2:33
  • @Plato please explain why?
    – nageeb
    Commented Feb 11, 2019 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.