Best way to select random rows PostgreSQL

前端 未结 12 989
借酒劲吻你
借酒劲吻你 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

    postgresql order by random(), select rows in random order:

    select your_columns from your_table ORDER BY random()
    

    postgresql order by random() with a distinct:

    select * from 
      (select distinct your_columns from your_table) table_alias
    ORDER BY random()
    

    postgresql order by random limit one row:

    select your_columns from your_table ORDER BY random() limit 1
    

提交回复
热议问题