MySQL Select Random Rows: A Beginner’s Guide – MySQL Tutorial

By | October 16, 2019

We usually use select statement to select rows from mysql database, however, these rows are ordered by primay key, not are orderded randomly. In this tutorial, we will introduce how to select random rows from mysql.

For example, we have a table called ‘book‘, we have stored some books in this table.

SELECT *
FROM `book`
LIMIT 0 , 30

The rows are:

mysql select rows

From rows, we can find these rows are ordered by primary key: book_id.

To make these rows are ordered randomly, we can do like this:

SELECT *
FROM `book`
order by random()
LIMIT 0 , 30

The rows are:

mysql select random rows

From rows, we can find these selected rows are ordered randomly.

Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *