本文介紹了ORDER BY 和 LIMIT 后的 UNION的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我的目標是執行兩個不同的查詢,然后將它們組合起來.
我的代碼是:
My goal is to execute two different queries and then combine them.
My code is:
SELECT * FROM some tables WHERE ... ORDER BY field1 LIMIT 0,1
UNION
SELECT * FROM some tables WHERE ...
我收到以下錯誤:
#1221 - UNION 和 ORDER BY 的錯誤使用
#1221 - Incorrect usage of UNION and ORDER BY
ORDER BY 僅用于第一個查詢很重要.我該如何執行此任務?
It is important that ORDER BY is only for the first query. How can I perform this task?
推薦答案
您可以使用括號來允許對單個查詢使用 ORDER
/LIMIT
:
You can use parenthesis to allow the use of ORDER
/LIMIT
on individual queries:
(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 */
這篇關于ORDER BY 和 LIMIT 后的 UNION的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!