My table has many row that have a same column. I'll call the column "likes". If I use "order by likes", then the rows with the same "likes" will be ordered by the time they were added to the database. I want all rows with the same "likes" to be sorted randomly. I tried "ORDER BY likes, rand()", but everything is being sorted randomly. How can I do this?
Asked
Active
Viewed 1,529 times
2
-
it would help to show the table definition and the query – Bohemian May 31 '11 at 03:47
-
possible duplicate of [How to request a random row in SQL?](http://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql) – theMayer Jan 12 '15 at 05:12
3 Answers
5
I suggest you check again. Adding the clause order by something, rand()
to one of my queries acts exactly as you would expect, with the only randomising happening within a something
group.
Just be aware that using rand()
in an order by
clause will not scale very well as your table gets bigger.

paxdiablo
- 854,327
- 234
- 1,573
- 1,953
0
It could be that you are doing it backwards (e.g., "ORDER BY RAND(), something" rather than "ORDER BY something, RAND())
0
Im not pretty sure since i dont know the structure of your tables and i dont know the query... but try adding GROUP BY likes
to your query

Throoze
- 3,988
- 8
- 45
- 67
-
3Although he wants results *grouped by* likes, `GROUP BY` would not be correct here; that gathers all similar items into a single row to show the result of aggregation functions. – Rob Kennedy May 31 '11 at 04:13