本文将为您介绍如何对数据随机取出数据的前 N 条数据。
示例数据
目前的数据,如下表所示:
empno | ename | job | sal |
---|---|---|---|
7369 | SMITH | CLERK | 800.0 |
7876 | SMITH | CLERK | 1100.0 |
7900 | JAMES | CLERK | 950.0 |
7934 | MILLER | CLERK | 1300.0 |
7499 | ALLEN | SALESMAN | 1600.0 |
7654 | MARTIN | SALESMAN | 1250.0 |
7844 | TURNER | SALESMAN | 1500.0 |
7521 | WARD | SALESMAN | 1250.0 |
实现方法
通过
order by rand
()来实现随机抽取效果。
SELECT empno
, ename
, sal
, job
FROM emp
order by rand() limit 3