如何在 MySQL 中检索一行随机行或多行随机行?


你可以使用 RAND() 方法来实现。要检索一行随机行,请使用以下语法

SELECT *FROM yourTableName ORDER BY RAND() LIMIT yourIntegerNumber;

为了了解上面的语法,让我们创建一个表。创建表的查询如下

mysql> create table gettingRandomRow
   -> (
   -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> CustomerName varchar(100)
   -> );
Query OK, 0 rows affected (0.45 sec)

使用插入命令在表中插入一些记录。查询如下 −

mysql> insert into gettingRandomRow(CustomerName) values('Chris');
Query OK, 1 row affected (0.14 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Robert');
Query OK, 1 row affected (0.10 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Ramit');
Query OK, 1 row affected (0.15 sec)
mysql> insert into gettingRandomRow(CustomerName) values('James');
Query OK, 1 row affected (0.11 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Jace');
Query OK, 1 row affected (0.12 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Mike');
Query OK, 1 row affected (0.15 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Sam');
Query OK, 1 row affected (0.17 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Carol');
Query OK, 1 row affected (0.11 sec)
mysql> insert into gettingRandomRow(CustomerName) values('David');
Query OK, 1 row affected (0.16 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Maxwell');
Query OK, 1 row affected (0.14 sec)
mysql> insert into gettingRandomRow(CustomerName) values('Larry');
Query OK, 1 row affected (0.13 sec)
mysql> insert into gettingRandomRow(CustomerName) values('John');
Query OK, 1 row affected (0.11 sec)

使用 select 语句显示表中的所有记录。查询如下 −

mysql> select *from gettingRandomRow;

以下是执行结果

+------------+--------------+
| CustomerId | CustomerName |
+------------+--------------+
|          1 | Chris        |
|          2 | Robert       |
|          3 | Ramit        |
|          4 | James        |
|          5 | Jace         |
|          6 | Mike         |
|          7 | Sam          |
|          8 | Bob          |
|          9 | Carol        |
|         10 | David        |
|         11 | Maxwell      |
|         12 | Larry        |
|         13 | John         |
+------------+--------------+
13 rows in set (0.00 sec)

以下是获取一行随机行或多行随机行的查询。

情况 1:查询如下 −

mysql> select *from gettingRandomRow order by rand() LIMIT 1;

以下是执行结果

+------------+--------------+
| CustomerId | CustomerName |
+------------+--------------+
|         13 | John         |
+------------+--------------+
1 row in set (0.00 sec)

情况 2:查询如下 −

mysql> select *from gettingRandomRow order by rand() LIMIT 2;

以下是执行结果

+------------+--------------+
| CustomerId | CustomerName |
+------------+--------------+
|          2 | Robert       |
|          3 | Ramit        |
+------------+--------------+
2 rows in set (0.00 sec)

情况 3:查询如下 −

mysql> select *from gettingRandomRow order by rand() LIMIT 2;

以下是执行结果

+------------+--------------+
| CustomerId | CustomerName |
+------------+--------------+
|          4 | James        |
|         9 | Carol         |
+------------+--------------+
2 rows in set (0.00 sec)

更新时间: 30-Jul-2019

163 次浏览

开启你的 职业生涯

通过完成课程获取认证

开始学习
广告