apply “ORDER BY” on a “UNION” (Mysql)

笑着哭i 提交于 2020-01-11 04:41:12

问题


Good Day. So, everythign 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 an union and order them. It didn't worked.

I looked around like here on Stack or here developpez (!!french website). I try the different exemple, and suggestion, but no success. It seems from what i red that it's because I am working on Mysql.

Anyway, here are my attemps, 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 teh frist select (odered 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 teh result were teh same as orginal request: result of first select (order 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 as attempt 1, but with a warning i the Mysql logic: (this type of close has been already analyzed (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

=> Do not ran, 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 

=> do not ran, and a nice list of 6 error. Any suggestion?


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/43742028/apply-order-by-on-a-union-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!