如何在 MySQL 中从最近的 20 行数据中随机选择 5 行?
要获得随机选择,使用 RAND() 方法。要限制行数,使用 LIMIT() 方法。
让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.51 sec)
使用 insert 命令向表中插入一些记录 −
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)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
这将产生以下输出 −
+---------------------+ | 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)
以下是从最近 20 行中随机选择 5 行的查询 −
mysql> select tbl1.* from (select *from DemoTable ORDER BY ShippingDate DESC LIMIT 20 ) as tbl1 -> ORDER BY RAND() LIMIT 5;
输出
这将产生以下输出 −
+---------------------+ | 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)
广告