
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to order results of a query randomly & select random rows in MySQL?
To order results of a query randomly, use ORDER BY RAND(). The syntax is as follows −
select * from DemoTable1559 where yourColumnName IN(yourValue1,yourValue2,....N) order by rand() limit yourLimitValue;
Let us first create a table −
mysql> create table DemoTable1559 -> ( -> EmployeeId int, -> EmployeeName varchar(20), -> EmployeeAge int -> ); Query OK, 0 rows affected (1.22 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1559 values(101,'Bob',28); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1559 values(102,'Robert',26); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1559 values(103,'Sam',25); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1559 values(104,'Mike',29); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1559 values(105,'John',34); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1559 values(106,'Carol',31); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1559;
This will produce the following output −
+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | 101 | Bob | 28 | | 102 | Robert | 26 | | 103 | Sam | 25 | | 104 | Mike | 29 | | 105 | John | 34 | | 106 | Carol | 31 | +------------+--------------+-------------+ 6 rows in set (0.00 sec)
Following is the query to order results of a query randomly and select random rows −
mysql> select * from DemoTable1559 where EmployeeId IN(101,103,106) order by rand() limit 3;
This will produce the following output −
+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | 101 | Bob | 28 | | 103 | Sam | 25 | | 106 | Carol | 31 | +------------+--------------+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- Randomly SELECT distinct rows in a MySQL table?
- Order randomly in MySQL with a random value column?
- MySQL query to select rows except first row in descending order?
- Select two random rows in a MySQL database?
- How to select MySQL rows in the order of IN clause?
- MySQL query to select too many rows?
- MySQL query to select multiple rows effectively?
- How to order and select query with conditions in MySQL?
- MySQL query to select rows older than a week?
- MySQL query to select top n rows efficiently?
- Get a fixed number of results in descending order using a MySQL query
- MySQL query to select rows one batch at a time
- How to select the last three rows of a table in ascending order with MySQL?
- MySQL query to select distinct order by id
- How do I select 5 random rows from the 20 most recent rows in MySQL?

Advertisements