Best way to select random rows PostgreSQL

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

    Starting with PostgreSQL 9.5, there's a new syntax dedicated to getting random elements from a table :

    SELECT * FROM mytable TABLESAMPLE SYSTEM (5);
    

    This example will give you 5% of elements from mytable.

    See more explanation on the documentation: http://www.postgresql.org/docs/current/static/sql-select.html

提交回复
热议问题