1. 程式人生 > 其它 >mysql>>隨機返回結果集 >>rand的使用

mysql>>隨機返回結果集 >>rand的使用

表結構
+
--------+------------+------------+-----------+--------+------------+ | emp_no | birth_date | first_name | last_name | gender | hire_date | +--------+------------+------------+-----------+--------+------------+ | 10001 | 1953-09-02 | Georgi | Facello | M | 1986-06-26 | | 10002 | 1964-06-02 | Bezalel |
Simmel | F | 1985-11-21 | | 10003 | 1959-12-03 | Parto | Bamford | M | 1986-08-28 | | 10004 | 1954-05-01 | Chirstian | Koblick | M | 1986-12-01 | | 10005 | 1955-01-21 | Kyoichi | Maliniak | M | 1989-09-12 | | 10006 | 1953-04-20 | Anneke | Preusig | F | 1989-06-02 | | 10007
| 1957-05-23 | Tzvetan | Zielinski | F | 1989-02-10 | | 10008 | 1958-02-19 | Saniya | Kalloufi | M | 1994-09-15 | | 10009 | 1952-04-19 | Sumant | Peac | F | 1985-02-18 | | 10010 | 1963-06-01 | Duangkaew | Piveteau | F | 1989-08-24 | +--------+------------+------------+-----------+--------+------------+
#隨機返回2行結果集
select
* from employees order by rand() limit 2;
+第一次的執行結果集

--------+------------+------------+-----------+--------+------------+ | emp_no | birth_date | first_name | last_name | gender | hire_date | +--------+------------+------------+-----------+--------+------------+ | 454095 | 1952-02-28 | Keiichiro | Kalefeld | M | 1985-04-10 | | 56999 | 1956-01-23 | Udi | Wiegley | M | 1985-09-26 | +--------+------------+------------+-----------+--------+------------+
+第二次執行結果集

--------+------------+------------+-----------+--------+------------+ | emp_no | birth_date | first_name | last_name | gender | hire_date | +--------+------------+------------+-----------+--------+------------+ | 231522 | 1955-02-08 | Saeed | Kaminger | M | 1995-07-12 | | 456029 | 1964-10-25 | Breannda | Haldar | M | 1991-07-23 | +--------+------------+------------+-----------+--------+------------+