當我們將 MySQL ORDER BY 子句與 RAND() 函數一起使用時,結果集將具有經過打亂的行集。換句話說,結果集將按隨機順序排列。要理解它,請考慮一個具有以下記錄的表“Employee” -
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)
現在,下面的查詢將使用ORDER BT RAND() 來打亂結果集中的行集-
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)
以上是如何在 ORDER BY 子句中使用 RAND() 函數來打亂 MySQL 行集?的詳細內容。更多資訊請關注PHP中文網其他相關文章!