Reputation: 28040
I am trying to find a way to pull 10 random records and then order those 10 records by a field. I have tried the following:
SELECT name FROM users ORDER BY RAND(), name LIMIT 10
but it does not order by name with 10 rows returned, just return 10 random record in any order. Is there a way to order by rand() and a field within a query with MySQL?
Upvotes: 3
Views: 1879
Reputation: 425411
SELECT name
FROM (
SELECT name
FROM users
ORDER BY
RAND()
LIMIT 10
) q
ORDER BY
name
Upvotes: 8