使用Oracle的rownum可以實現一些有用的技巧,例如:
SELECT * FROM (
SELECT rownum AS rn, t.* FROM table_name t
) WHERE rn <= 10;
SELECT * FROM table_name ORDER BY dbms_random.value FETCH FIRST 10 ROWS ONLY;
SELECT * FROM (
SELECT rownum AS rn, t.* FROM table_name t GROUP BY column_name
) WHERE rn = 1;
SELECT * FROM table_name WHERE rownum <= 10;
需要注意的是,rownum是在查詢結果返回之前計算的,所以在使用rownum時需要注意查詢結果的排序和過濾條件,以避免結果不符合預期。