select * from (
select name, age, sex, phone, email, score
from member
order by score
)
where rownum <= 100
위와 같은 쿼리를 날리면
member 테이블에서 score를 기준으로 100명을 뽑는다.
member 테이블에 약 5만명정도 들어있다고 치자. 근데 여기서 랜덤으로 100명만 뽑고 싶다!
그럼 어떻게 해야되는가? (새로고침할때마다 바뀌게)
select * from (
select MOD(ABS(DBMS_RANDOM.RANDOM),50000) as jhs, name, age, sex, phone, email, score
from member
order by jhs
)
where rownum <= 100
요렇게 하면 랜덤으로 100명 뽑는다. 물론 새로고침할때마다 바뀐다.
이걸 응용해서 로또 알고리즘을 만들 수 있다.
테이블에 no라는 필드를 만들고 1부터 46까지 데이터를 넣는다.
create table lotto_table
(
no number(2)
);
begin
insert into lotto_table values (1);
insert into lotto_table values (2);
insert into lotto_table values (3);
insert into lotto_table values (4);
insert into lotto_table values (5);
(중간생략)
insert into lotto_table values (46);
end;
그 다음에
select * from (
select MOD(ABS(DBMS_RANDOM.RANDOM),46) as jhs, no
from lotto_table
order by jhs
)
where rownum <= 6
이렇게 하면 또 하나의 로또 알고리즘 완성!
이걸 잘 활용하면 써먹을 때가 많다.
난 이걸 기반으로 우리 회사 전산추첨 알고리즘을 짰다.