Tutorialspoint

How do I select 5 random rows from the 20 most recent rows in MySQL?


1 Answer
Sharon Christine

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;

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;

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)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.