我的目标是执行两个不同的查询,然后将它们组合起来。
我的代码是:
SELECT * FROM some tables WHERE ... ORDER BY field1 LIMIT 0,1
UNION
SELECT * FROM some tables WHERE ...
我收到以下错误:
#1221 - Incorrect usage of UNION and ORDER BY
重要的是 ORDER BY 仅用于第一个查询。我该如何执行此任务?
最佳答案
您可以使用括号来允许对单个查询使用 ORDER
/LIMIT
:
(SELECT * FROM some tables WHERE ... ORDER BY field1 LIMIT 0, 1)
UNION
(SELECT * FROM some tables WHERE ...)
ORDER BY 1 /* optional -- applies to the UNIONed result */
LIMIT 0, 100 /* optional -- applies to the UNIONed result */
关于mysql - UNION 在 ORDER BY 和 LIMIT 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198016/