I saw many topics about this subject and I have been unsuccessful in understanding how to do it.
For example, if I have this table:
+------+-------+-------+
| id | name | class |
+------+-------+-------+
| 5 | test | one |
| 10 | test2 | one |
| 12 | test5 | one |
| 7 | test6 | two |
+------+-------+-------+
and I want to show only X random rows from class "one", how can I do that?
NOTE: it's a big table, so I don't want to use ORDER BY RAND
.
The
ORDER BY RAND()
solution that most people recommend doesn't scale to large tables, as you already know.I cover this and other solutions in my book, SQL Antipatterns: Avoiding the Pitfalls of Database Programming.
If you want to do this with PHP, you could do something like this (not tested):
ordering by
rand()
is not particularly efficient, but it's about the smallest/quickest way of doing it.