Selecting random rows

前端 未结 3 1638
误落风尘
误落风尘 2021-01-22 19:45

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

相关标签:
3条回答
  • 2021-01-22 20:16

    Try this !

    $ select * from table_name order by random() limit 3 ;
    
    0 讨论(0)
  • 2021-01-22 20:19

    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.

    0 讨论(0)
  • 2021-01-22 20:36

    Use the random function.

    SELECT * FROM tablename ORDER BY random() LIMIT 3;
    
    0 讨论(0)
提交回复
热议问题