开发者

UNION and ORDER BY issue in MySQL

开发者 https://www.devze.com 2023-04-10 01:49 出处:网络
You should see what I\'m trying to do here but it\'s not working $getquery = \"SELECT * FROM highscore

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".


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号