
- 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 do I select 5 random rows from the 20 most recent rows in MySQL?
For random, use RAND() method. And for limit on rows, use the LIMIT() method.
Let us first create a table −
mysql> create table DemoTable -> ( -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-01-03'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-01-05'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-01-07'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-02-02'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-02-04'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-02-12'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('2019-02-24'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-02-25'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-03-10'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-03-11'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-03-12'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-03-14'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-03-16'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-03-18'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-03-20'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-03-25'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-03-28'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-03-29'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-04-03'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2019-04-03'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-04-04'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-04-06'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-04-09'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-05-01'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('2019-05-13'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('2019-05-15'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-05-16'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-05-18'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+---------------------+ | ShippingDate | +---------------------+ | 2019-01-01 00:00:00 | | 2019-01-03 00:00:00 | | 2019-01-05 00:00:00 | | 2019-01-07 00:00:00 | | 2019-02-02 00:00:00 | | 2019-02-04 00:00:00 | | 2019-02-12 00:00:00 | | 2019-02-24 00:00:00 | | 2019-02-25 00:00:00 | | 2019-03-10 00:00:00 | | 2019-03-11 00:00:00 | | 2019-03-12 00:00:00 | | 2019-03-14 00:00:00 | | 2019-03-16 00:00:00 | | 2019-03-18 00:00:00 | | 2019-03-20 00:00:00 | | 2019-03-25 00:00:00 | | 2019-03-28 00:00:00 | | 2019-03-29 00:00:00 | | 2019-04-03 00:00:00 | | 2019-04-03 00:00:00 | | 2019-04-04 00:00:00 | | 2019-04-06 00:00:00 | | 2019-04-09 00:00:00 | | 2019-05-01 00:00:00 | | 2019-05-13 00:00:00 | | 2019-05-15 00:00:00 | | 2019-05-16 00:00:00 | | 2019-05-18 00:00:00 | +---------------------+ 29 rows in set (0.00 sec)
Following is the query to select 5 random rows from the 20 most recent rows −
mysql> select tbl1.* from (select *from DemoTable ORDER BY ShippingDate DESC LIMIT 20 ) as tbl1 -> ORDER BY RAND() LIMIT 5;
Output
This will produce the following output −
+---------------------+ | ShippingDate | +---------------------+ | 2019-03-14 00:00:00 | | 2019-05-13 00:00:00 | | 2019-03-25 00:00:00 | | 2019-05-16 00:00:00 | | 2019-03-10 00:00:00 | +---------------------+ 5 rows in set (0.04 sec)
- Related Articles
- Select only 5 random rows in the last 50 entries With MySQL?
- How do I SELECT none of the rows and columns in MySQL?
- Select two random rows in a MySQL database?
- What is the most efficient way to select a specific number of random rows in MySQL?
- How do I delete blank rows in MySQL?
- How to select last 10 rows from MySQL?
- How select specific rows in MySQL?
- How to order results of a query randomly & select random rows in MySQL?
- Select all rows except from today in MySQL?
- Fetch random rows from a table with MySQL
- How to select last two rows in MySQL?
- Select rows from a MySQL table and display using IN()
- How to retrieve a random row or multiple random rows in MySQL?
- MySQL Select Rows where two columns do not have the same value?
- How do I select four random tables from a MySQL database having thousands of tables?

Advertisements