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;
阅读更多:MySQL 教程
输出
这将产生以下输出 –
+---------------------+
| 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)