码迷,mamicode.com
首页 > 其他好文 > 详细

从表中随机返回n条记录

时间:2015-10-12 22:32:16      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

创建测试用表:

CREATE OR REPLACE VIEW V AS
SELECT ‘a‘ AS c FROM dual
UNION ALL
SELECT ‘b‘ AS c FROM dual
UNION ALL
SELECT ‘c‘ AS c FROM dual
UNION ALL
SELECT ‘d‘ AS c FROM dual
UNION ALL
SELECT ‘e‘ AS c FROM dual;
SELECT * FROM v;

技术分享

查询语句如下:

SELECT c
  FROM (SELECT c FROM v ORDER BY DBMS_RANDOM.VALUE())
 WHERE ROWNUM < 3;

查询结果如下:

技术分享

从表中随机返回n条记录

标签:

原文地址:http://www.cnblogs.com/minisculestep/p/4872780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!