
- 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
Select random row that exists in a MySQL table?
To select random value, use rand(). Here, we are selecting only a single random value, therefore, LIMIT 1 is used. Following is the syntax −
select *from yourTableName order by rand() limit 1;
Let us first create a table −
mysql> create table DemoTable735 (Amount int); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable735 values(984474734); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable735 values(489393323); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable735 values(23432333); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable735 values(8949933); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable735;
This will produce the following output -
+-----------+ | Amount | +-----------+ | 984474734 | | 489393323 | | 23432333 | | 8949933 | +-----------+ 4 rows in set (0.00 sec)
Following is the query to select random row in MySQL
mysql> select *from DemoTable735 order by rand() limit 1;
This will produce the following output -
+-----------+ | Amount | +-----------+ | 489393323 | +-----------+ 1 row in set (0.00 sec)
- Related Articles
- Select a random row in MySQL
- Display random row from a MySQL table
- Check that a table exists in MySQL?
- Best way to test if a row exists in a MySQL table
- MySQL query to select one specific row and another random row?\n
- Select a fixed number of random records from a MySQL table?
- MySQL select query to select rows from a table that are not in another table?
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- Select two random rows in a MySQL database?
- How to retrieve a random row or multiple random rows in MySQL?
- MySQL SELECT from table A that does not exist in table B using JOINS?
- How to select from MySQL table A that does not exist in table B?
- MySQL LIMIT to select a single row
- Select random number from a specific list in MySQL?
- Inserting random numbers into a table in MySQL?

Advertisements