开发者

Selecting random rows

开发者 https://www.devze.com 2023-02-11 20:38 出处:网络
Hi all, My requirement is simple.I want to select random rows from a table. For example my table having 10开发者_高级运维 rows I want to select any three rows randomly.Is there any way in psql.Use the

Hi all, My requirement is simple.I want to select random rows from a table.

For example my table having 10开发者_高级运维 rows I want to select any three rows randomly.Is there any way in psql.


Use the random function.

SELECT * FROM tablename ORDER BY random() LIMIT 3;


Please be aware that once your table grows the "order by random/limit" approach will be slow, since it requires a whole table scan.

See http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/ for an alternative solution.


Try this !

$ select * from table_name order by random() limit 3 ;
0

精彩评论

暂无评论...
验证码 换一张
取 消