ryanzec
ryanzec

Reputation: 28040

Ordering MySQL Query By Random and a Field?

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

Answers (2)

ryanzec
ryanzec

Reputation: 28040

Ended up just doing the sort in php.

Upvotes: 1

Quassnoi
Quassnoi

Reputation: 425411

SELECT  name
FROM    (
        SELECT  name
        FROM    users
        ORDER BY
                RAND()
        LIMIT 10
        ) q
ORDER BY
        name

Upvotes: 8

Related Questions