Best way to select random rows PostgreSQL

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

    Here is a decision that works for me. I guess it's very simple to understand and execute.

    SELECT 
      field_1, 
      field_2, 
      field_2, 
      random() as ordering
    FROM 
      big_table
    WHERE 
      some_conditions
    ORDER BY
      ordering 
    LIMIT 1000;
    

提交回复
热议问题