
- 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
Selecting random entry from MySQL Database?
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientAge int ); Query OK, 0 rows affected (0.92 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ClientName,ClientAge) values('Robert',45); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(ClientName,ClientAge) values('Mike',55); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ClientName,ClientAge) values('Bob',42); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(ClientName,ClientAge) values('Sam',47); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+------------+-----------+ | Id | ClientName | ClientAge | +----+------------+-----------+ | 1 | Robert | 45 | | 2 | Mike | 55 | | 3 | Bob | 42 | | 4 | Sam | 47 | +----+------------+-----------+ 4 rows in set (0.00 sec)
Following is the query to select a random entry from MySQL database −
mysql> select *from DemoTable order by rand() limit 1;
This will produce the following output −
+----+------------+-----------+ | Id | ClientName | ClientAge | +----+------------+-----------+ | 3 | Bob | 42 | +----+------------+-----------+ 1 row in set (0.00 sec)
- Related Articles
- Selecting Random Result from MySQL?
- Creating and Selecting a MySQL Database
- How to select a random record from a MySQL database?
- Select two random rows in a MySQL database?
- How do I select four random tables from a MySQL database having thousands of tables?
- Selecting database inside the JS in MongoDB?
- Need help selecting non-empty column values from MySQL?
- Selecting from a MySQL table based on parts of a timestamp?
- Create a MySQL table from already created table selecting specific rows?
- Selecting data from a MySQL table based on a specific month?
- Display random row from a MySQL table
- Create a database in MySQL from Java?
- Retrieving MySQL Database structure information from Java?
- Connect to MySQL database from command line
- Fetch random rows from a table with MySQL

Advertisements