我有两个查询,每个查询都有自己的 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
将它们组合起来?
最佳答案
您需要按以下方式对结果进行排序:
- 结果类型(MATCH 或 LIKE)
- 标题(用于 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
关于mysql - 如何使用 UNION 合并两个具有 ORDER BY 的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33149552/