Posted By

aamirrajpoot on 04/20/11


Tagged

mysql rand


Versions (?)

Order by rand() Reduce time II


 / Published in: PHP
 

Another version for my old Order by Rand function now it includes some PHP

  1. //This is pseudo code for fetching 1 random row. I will update to add a new solution to fetch more random rows.
  2. function randomRow(table, Idcolumn) {
  3. maxRow = query("SELECT MAX(Idcolumn) AS maxID FROM table");
  4. if (maxRow.recordCount > 0) {
  5. do {
  6. randomID = randRange(1, maxRow.maxID);
  7. randomRow = query("SELECT * FROM table WHERE column = randomID");
  8. } while (randomRow.recordCount == 0);
  9. return randomRow;
  10. } else {
  11. return false;
  12. }
  13. }

Report this snippet  

You need to login to post a comment.