如何使用 UNION 合并两个具有 ORDER BY 的查询?
How to combine two queries that have ORDER BY using UNION?
我有两个查询,每个查询都有自己的 order by
,如下所示:
查询 1:
SELECT id, name, title, content
FROM table where match(title, content) against('anything')
Order By title
查询 1:
SELECT id, tag, question, answer
FROM table
Where tag like '%anything'
Order By tag, question
现在如何使用 UNION ALL
组合它们?
如果您想保持相同的顺序,那么通常可以使用以下方法:
(SELECT id, name, title, content
FROM table
where match(title, content) against('anything')
order by title
) union all
(SELECT id, tag, question, answer
FROM table
where tag like '%anything'
order by tag, question
);
这在实践中有效,因为在实践中第一个子查询在第二个之前执行。但是,我认为 MySQL 文档不能保证两者的处理顺序。为了保证,你需要一个外部 order by
:
(SELECT id, name, title, content, 1 as priority
FROM table
where match(title, content) against('anything')
) union all
(SELECT id, tag, question, answer, 2 as prioirty
FROM table
where tag like '%anything'
)
ORDER BY priority, title, content
您需要按以下方式对结果进行排序:
- 结果类型(匹配或喜欢)
- 标题(对于 MATCH)或标签(对于 LIKE)
- NULL(对于 MATCH)或问题(对于 LIKE)
您可以使用嵌套查询:
SELECT * FROM (
SELECT 1 AS result_type, id, name, title, content
FROM table
WHERE MATCH (title, content) AGAINST ('anything')
UNION ALL
SELECT 2, id, tag, question, answer
FROM table
WHERE tag LIKE '%anything'
) AS foobar
ORDER BY
result_type,
CASE result_type WHEN 1 THEN title ELSE tag END,
CASE result_type WHEN 1 THEN NULL ELSE question END
或者您可以添加排序辅助列:
(
SELECT 1 AS sort_1, title AS sort_2, NULL AS sort_3, id, name, title, content
FROM table
WHERE MATCH (title, content) AGAINST ('anything')
) UNION ALL (
SELECT 2 AS sort_1, tag AS sort_2, question AS sort_3, id, tag, question, answer
FROM table
WHERE tag LIKE '%anything'
)
ORDER BY sort_1, sort_2, sort_3
我有两个查询,每个查询都有自己的 order by
,如下所示:
查询 1:
SELECT id, name, title, content
FROM table where match(title, content) against('anything')
Order By title
查询 1:
SELECT id, tag, question, answer
FROM table
Where tag like '%anything'
Order By tag, question
现在如何使用 UNION ALL
组合它们?
如果您想保持相同的顺序,那么通常可以使用以下方法:
(SELECT id, name, title, content
FROM table
where match(title, content) against('anything')
order by title
) union all
(SELECT id, tag, question, answer
FROM table
where tag like '%anything'
order by tag, question
);
这在实践中有效,因为在实践中第一个子查询在第二个之前执行。但是,我认为 MySQL 文档不能保证两者的处理顺序。为了保证,你需要一个外部 order by
:
(SELECT id, name, title, content, 1 as priority
FROM table
where match(title, content) against('anything')
) union all
(SELECT id, tag, question, answer, 2 as prioirty
FROM table
where tag like '%anything'
)
ORDER BY priority, title, content
您需要按以下方式对结果进行排序:
- 结果类型(匹配或喜欢)
- 标题(对于 MATCH)或标签(对于 LIKE)
- NULL(对于 MATCH)或问题(对于 LIKE)
您可以使用嵌套查询:
SELECT * FROM (
SELECT 1 AS result_type, id, name, title, content
FROM table
WHERE MATCH (title, content) AGAINST ('anything')
UNION ALL
SELECT 2, id, tag, question, answer
FROM table
WHERE tag LIKE '%anything'
) AS foobar
ORDER BY
result_type,
CASE result_type WHEN 1 THEN title ELSE tag END,
CASE result_type WHEN 1 THEN NULL ELSE question END
或者您可以添加排序辅助列:
(
SELECT 1 AS sort_1, title AS sort_2, NULL AS sort_3, id, name, title, content
FROM table
WHERE MATCH (title, content) AGAINST ('anything')
) UNION ALL (
SELECT 2 AS sort_1, tag AS sort_2, question AS sort_3, id, tag, question, answer
FROM table
WHERE tag LIKE '%anything'
)
ORDER BY sort_1, sort_2, sort_3