Leon Anavi
IT Tips && Tricks

Databases

Created: 02.09.2009 23:48 Last Modified: 08.09.2009 11:25 Views: 7702
Keywords: MySQL, random, select

MySQL: Select Random Rows

Solution

Selecting random rows from a MySQL database table can be useful in order to display advertisements or any other kind of specific information. To extract random rows use a SELECT statement ordered by the RAND() clause.

Example

SELECT * FROM users ORDER BY RAND() LIMIT 1;
The example query will extract all columns of a single row of table users. To select more rows increase the LIMIT of the query.

Disadvantages

The presented method for extracting random rows might be slow for tables consist of more than a million rows.

Reference Manual

MySQL Select Syntax


  Home | About | Contact | Disclaimer | Sitemap © 2009-2022 Leon Anavi. All rights reserved.