Eos the SeaCat Eos the SeaCat - 3 months ago 6
MySQL Question

I need to select top five and bottom five numbers from a list of 60 numbers

I need to select top five and bottom five numbers from a list of 60 numbers based on their count.

I have a table - scores
query to select top five

SELECT * FROM scores ORDER BY count DESC LIMIT 5


gets the top five

SELECT * FROM scores ORDER BY count ASC LIMIT 5


gets both sets of numbers -

what would be the best way to combine these two queries to return one set (make these into one query)

join?

I have tried this but get an error on the order by

SELECT * FROM scores order by count desc limit 5 union all select * from scores order by count asc limit 3

Answer

You can use UNION ALL:

(SELECT * 
FROM scores 
ORDER BY count DESC LIMIT 5) 

UNION ALL

(SELECT * 
FROM scores 
ORDER BY count ASC LIMIT 5)