

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Selecting Random Result from MySQL?
You need to use rand() function to select random result from MySQL.
The syntax is as follows
select *from yourTableName order by rand() limit 1;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table selectRandomRecord -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into selectRandomRecord(StudentName) values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectRandomRecord(StudentName) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into selectRandomRecord(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectRandomRecord(StudentName) values('Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectRandomRecord(StudentName) values('Mike'); Query OK, 1 row affected (0.16 sec) mysql> insert into selectRandomRecord(StudentName) values('Robert'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from selectRandomRecord;
The following is the output
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | | 5 | Mike | | 6 | Robert | +-----------+-------------+ 6 rows in set (0.00 sec)
The following is the query to select random result from MySQL.
mysql> select *from selectRandomRecord order by rand() limit 1;
The following is the output
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 3 | Bob | +-----------+-------------+ 1 row in set (0.00 sec)
Now execute the same query again to get another random value
mysql> select *from selectRandomRecord order by rand() limit 1;
The following is the output
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 5 | Mike | +-----------+-------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- Selecting random entry from MySQL Database?
- Need help selecting non-empty column values from MySQL?
- Display random row from a MySQL table
- Selecting and dividing numbers in a column and displaying the decimal result in integer with MySQL
- Selecting data from a MySQL table based on a specific month?
- Selecting from a MySQL table based on parts of a timestamp?
- Create a MySQL table from already created table selecting specific rows?
- Selecting Elements from Lists in Perl
- Selecting a single row in MySQL?
- Creating and Selecting a MySQL Database
- Fetch random rows from a table with MySQL
- Set user variable from result of query in MySQL?
- MySQL query to get result from multiple select statements?
- Selecting only a single field from MongoDB?
- Select random number from a specific list in MySQL?
Advertisements