Home > Database > Mysql Tutorial > 使用Oracle分析函数随机抽取N条记录

使用Oracle分析函数随机抽取N条记录

WBOY
Release: 2016-06-07 16:43:33
Original
1277 people have browsed it

模拟一个歌曲表,有歌曲ID和作者ID,并且初始化一些数据 create table music( music_id int primary key, userid int );

模拟一个歌曲表,有歌曲ID和作者ID,,并且初始化一些数据

create table music(
 
    music_id int primary key,

    userid int

);

insert into music(music_id)

select rownum rn from dual connect by level

update music set userid= mod(music_id,5);

commit;
 要求:
 每个作者随机抽取他的3个作品。
 
SQL如下
 

select * from
 
(

    select music_id,userid,ROW_NUMBER() OVER (PARTITION BY userid ORDER BY dbms_random.random()) rn from music

)

where rn 
第一次执行:

使用Oracle分析函数随机抽取N条记录

第二次执行:

使用Oracle分析函数随机抽取N条记录

这个方法只针对Oracle有效(使用了它的分析函数)

Oracle函数之GREATEST函数详解实例

Oracle函数之单行转换函数

生成动态前缀且自增号码的Oracle函数

Oracle函数之Replace()

Oracle函数大全

Oracle函数之case和decode的用法区别及性能比较

本文永久更新链接地址:

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template