在 "UNION" (Mysql) 上应用 "ORDER BY"

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

所以,一切都在标题中。

我希望合并两个请求的结果并将结果排序在一起(例如,不是一个接一个)。 => 我正在考虑申请工会并订购它们。 没用。

我环顾四周on Stack or here developpez (!!french website)。我尝试了不同的例子和建议,但没有成功。 从我读到的内容看来,这是因为我正在研究 Mysql.

无论如何,这是我的尝试和结果。

我最初的 2 个请求:

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

这个列表的结果由第一个 select 的结果(按索引键排序)后跟第二个 select 的结果按索引键排序。

尝试 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)

请求运行,但结果与原始请求相同:第一个select的结果(按索引键排序)后跟第二个请求的结果。

尝试 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

请求 运行,结果与尝试 1 相同,但在 Mysql 逻辑中带有警告:
(这种close已经分析过了(ORDER BY))

尝试 3:

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

没有 运行,而是错误 #1064 - UNION 附近的语法错误。

尝试 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 

没有 运行,还有 6 个错误的不错列表。 有什么建议吗?

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

您必须为您的 select 分配一个别名。但在这种情况下,UNION 不是必需的,可以用简单的 OR 代替,正如@Karoly Horvath 在他的评论中指出的那样。结果查询将如下所示:

SELECT 
 * 
FROM user_relation 
WHERE from_user_id = 1 OR to_user_id = 1 
ORDER BY trust_degree

写在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.

通过将上述信息应用于您的查询,它变成:

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

扩展我的评论:

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

如果您需要按更多字段排序,只需添加字段编号,ORDER BY 1, 2, 3 LIMIT 10;