Select random rows from a MySQL table


If you ever come across the need of randomly select rows from a MySQL table:

SELECT * FROM table WHERE field1 = value ORDER BY RAND() LIMIT x

Explanation:

table - table name

field1 - table field

value - value you want field1 to be

x – number of rows to return

Example:

SELECT * FROM tusers WHERE active_user = ‘Yes‘ ORDER BY RAND() LIMIT 10

This command returns 10 random rows of active users from table tusers.

  1. No comments yet.
(will not be published)