Select*fromemployee;+----+--------+--------+|ID|Name |Salary|+----+--------+--------+|1 |Gaurav|50000 ||2 |Rahul |20000&n"/> Select*fromemployee;+----+--------+--------+|ID|Name |Salary|+----+--------+--------+|1 |Gaurav|50000 ||2 |Rahul |20000&n">

Home  >  Article  >  Database  >  In MySQL, how do we randomize a set of rows or values ​​in a result set?

In MySQL, how do we randomize a set of rows or values ​​in a result set?

王林
王林forward
2023-09-05 09:49:081532browse

在 MySQL 中,我们如何随机化结果集中的行集或值集?

The RAND() function used with the ORDER BY clause allows you to randomize the set of rows or values ​​in the result set. To understand it, consider a table "Employee" with the following records -

mysql> Select * from employee;
+----+--------+--------+
| ID | Name   | Salary |
+----+--------+--------+
| 1  | Gaurav | 50000  |
| 2  | Rahul  | 20000  |
| 3  | Advik  | 25000  |
| 4  | Aarav  | 65000  |
| 5  | Ram    | 20000  |
| 6  | Mohan  | 30000  |
| 7  | Aryan  | NULL   |
| 8  | Vinay  | NULL   |
+----+--------+--------+
8 rows in set (0.00 sec)

Now, the following query will use ORDER BT RAND() to shuffle the set of rows in the result set -

mysql> Select * from Employee ORDER BY RAND();
+----+--------+--------+
| ID | Name   | Salary |
+----+--------+--------+
| 4  | Aarav  | 65000  |
| 1  | Gaurav | 50000  |
| 3  | Advik  | 25000  |
| 7  | Aryan  | NULL   |
| 6  | Mohan  | 30000  |
| 8  | Vinay  | NULL   |
| 5  | Ram    | 20000  |
| 2  | Rahul  | 20000  |
+----+--------+--------+
8 rows in set (0.00 sec)

The above is the detailed content of In MySQL, how do we randomize a set of rows or values ​​in a result set?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Surrogate keys in RDBMSNext article:Surrogate keys in RDBMS