
- 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
Fetch random rows from a table with MySQL
For this, you can use a PREPARE statement. Let us first create a table −
mysql> create table DemoTable( FirstName varchar(100), CountryName varchar(100) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam','US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Chris','AUS'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert','UK'); Query OK, 1 row affected (0.32 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | FirstName | CountryName | +-----------+-------------+ | Adam | US | | Chris | AUS | | Robert | UK | +-----------+-------------+ 3 rows in set (0.00 sec)
Following is the query to fetch random rows in a single query −
mysql> set @value := ROUND((select count(*) from DemoTable) * rand()); Query OK, 0 rows affected (0.00 sec) mysql> set @query := CONCAT('select *from DemoTable LIMIT ', @value , ', 1'); Query OK, 0 rows affected (0.00 sec) mysql> prepare myStatement from @query ; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> execute myStatement; +-----------+-------------+ | FirstName | CountryName | +-----------+-------------+ | Chris | AUS | +-----------+-------------+ 1 row in set (0.00 sec) mysql> deallocate prepare myStatement; Query OK, 0 rows affected (0.00 sec)
- Related Articles
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- How to fetch random rows in MySQL with comma separated values?
- Display random row from a MySQL table
- MySQL query to fetch the latest date from a table with date records
- Query MySQL table and fetch rows posted before the last 3 days?
- Create a MySQL table from already created table selecting specific rows?
- How to fetch the newly added records from a MySQL table?
- How will you extract multiple rows from a DB2 table in a single FETCH call?
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- Delete only specific rows in a table with MySQL
- Select a fixed number of random records from a MySQL table?
- How can we delete multiple rows from a MySQL table?
- How can we delete all rows from a MySQL table?
- Select rows from a MySQL table and display using IN()
- How can we fetch all the records from a particular MySQL table?

Advertisements