Home >Database >Mysql Tutorial >How to Efficiently Select Random Rows in SQLite?
Selecting Random Rows in SQLite
In most SQL databases, random row selection can be achieved using the ORDER BY RAND() function. However, in SQLite, this method is not supported.
Alternative Approach for SQLite
To select random rows in SQLite, a more efficient approach is recommended:
SELECT * FROM table WHERE id IN (SELECT id FROM table ORDER BY RANDOM() LIMIT x)
Improved Performance with Indexed Fields
Utilizing the id field for random ordering in the subquery provides significant performance gains:
The above is the detailed content of How to Efficiently Select Random Rows in SQLite?. For more information, please follow other related articles on the PHP Chinese website!