gpt4 book ai didi

mysql - UNION 在 ORDER BY 和 LIMIT 之后

转载 作者:IT老高 更新时间:2023-10-28 23:50:27 24 4
gpt4 key购买 nike

我的目标是执行两个不同的查询,然后将它们组合起来。
我的代码是:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com