Random Rows in SQL

At least in MySQL, all you have to do to return random data from your query is add ORDER BY RAND() to your query, for instance:

  SELECT * FROM table ORDER BY RAND() LIMIT 1

This example, for instance, returns an entire row from table. If you want more random rows, simply increase the LIMIT. You can, naturally, also make use of the WHERE clause, and you can of course choose the fields to include as well.

By using this code you can select an entirely random row from your table and get data where you need it. The only problem you have then is deciding where you need it most and how to make the best use of what you get back.


Posted

in