Best way to select random rows PostgreSQL

前端 未结 12 1125
借酒劲吻你
借酒劲吻你 2020-11-22 06:57

I want a random selection of rows in PostgreSQL, I tried this:

select * from table where random() < 0.01;

But some other recommend this:

12条回答
  •  无人共我
    2020-11-22 07:34

    If you want just one row, you can use a calculated offset derived from count.

    select * from table_name limit 1
    offset floor(random() * (select count(*) from table_name));
    

提交回复
热议问题