UNION and ORDER BY issue in MySQL

点点圈 提交于 2019-12-12 12:43:33

问题


You should see what I'm trying to do here but it's not working

$getquery = "SELECT * 
FROM highscore 
WHERE score >= '$score'
ORDER BY score ASC
LIMIT 6

UNION

SELECT * 
FROM highscore 
WHERE score < '$score'
ORDER BY score DESC
LIMIT 5";

mysql_error() returns: "improper usage of ORDER BY and UNION".


回答1:


Try:

$getquery = "(SELECT * 
FROM highscore 
WHERE score >= '$score'
ORDER BY score ASC
LIMIT 6)

UNION ALL -- guaranteed to be beneficial in this case as Johan commented

(SELECT * 
FROM highscore 
WHERE score < '$score'
ORDER BY score DESC
LIMIT 5)";

See the comments to my answer on the related question.
Or consult the fine manual.




回答2:


To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:

(SELECT * 
 FROM highscore 
 WHERE score >= '$score'
 ORDER BY score ASC
 LIMIT 6)

UNION

(SELECT * 
 FROM highscore 
 WHERE score < '$score'
 ORDER BY score DESC
 LIMIT 5)


来源:https://stackoverflow.com/questions/7638545/union-and-order-by-issue-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!