2

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?

Linksku
  • 1,449
  • 6
  • 17
  • 22

3 Answers3

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
  • 3
    Although 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