I have a table in MySQL that contains a column name category
.
I am trying to write a query that will return 2 random records from each category
.
Here is code that I use to get 2 records from each category
with the highest values in my rating
column:
SELECT e1.*
FROM entries AS e1
WHERE (SELECT Count(*)
FROM entries AS e2
WHERE e2.category = e1.category
AND e1.rating <= e2.rating) <= 2
ORDER BY category,
rating DESC
Check this link out to see a table with some sample data and the above query: http://sqlfiddle.com/#!9/bab8e/1