Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNION and ORDER BY issue in MySQL

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

like image 851
Tules Avatar asked Dec 28 '25 06:12

Tules


1 Answers

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.

like image 86
Erwin Brandstetter Avatar answered Dec 30 '25 22:12

Erwin Brandstetter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!