안녕하세요.
테이블에 있는 레코드 중 일부를 무작위로 추출해야 합니다.
Firebird FAQ 사이트에서 검색하니 아래와 같은 예가 있는데요.
ORDER BY 의 조건식이 언제나 0이 되지 않나요?
0을 만들기 위해 이렇게 복잡하게 쓰진 않았을 것 같구요.
아직 DB가 없어 확인해 보진 못했지만, 고수님의 답변 기다립니다.
How to select a random record from a table?
There is no such feature in Firebird, but you can use some tricks. The following example requires that you have a unique integer column (primary key is usually used):
SELECT ...field_list...
FROM table t1
WHERE conditions
ORDER BY (t1.int_col + seed)*4294967291-((t1.int_col + seed)*4294967291/49157)*49157;
If you just need one random record, limit the result set using FIRST or ROWS clause. This query will give consistent records for the same seed. If you wish to be completely random, you need to change the seed. You could use the value of int_col from previous run, or simply fetch a new value from a generator (just make sure the same value for seed is used in both places in expression).
|