How to request a random row in SQL?

前端 未结 29 2946
孤城傲影
孤城傲影 2020-11-21 06:45

How can I request a random row (or as close to truly random as is possible) in pure SQL?

相关标签:
29条回答
  • 2020-11-21 07:08

    See this post: SQL to Select a random row from a database table. It goes through methods for doing this in MySQL, PostgreSQL, Microsoft SQL Server, IBM DB2 and Oracle (the following is copied from that link):

    Select a random row with MySQL:

    SELECT column FROM table
    ORDER BY RAND()
    LIMIT 1
    

    Select a random row with PostgreSQL:

    SELECT column FROM table
    ORDER BY RANDOM()
    LIMIT 1
    

    Select a random row with Microsoft SQL Server:

    SELECT TOP 1 column FROM table
    ORDER BY NEWID()
    

    Select a random row with IBM DB2

    SELECT column, RAND() as IDX 
    FROM table 
    ORDER BY IDX FETCH FIRST 1 ROWS ONLY
    

    Select a random record with Oracle:

    SELECT column FROM
    ( SELECT column FROM table
    ORDER BY dbms_random.value )
    WHERE rownum = 1
    
    0 讨论(0)
  • 2020-11-21 07:08

    With SQL Server 2012+ you can use the OFFSET FETCH query to do this for a single random row

    select  * from MyTable ORDER BY id OFFSET n ROW FETCH NEXT 1 ROWS ONLY
    

    where id is an identity column, and n is the row you want - calculated as a random number between 0 and count()-1 of the table (offset 0 is the first row after all)

    This works with holes in the table data, as long as you have an index to work with for the ORDER BY clause. Its also very good for the randomness - as you work that out yourself to pass in but the niggles in other methods are not present. In addition the performance is pretty good, on a smaller dataset it holds up well, though I've not tried serious performance tests against several million rows.

    0 讨论(0)
  • 2020-11-21 07:08

    I have to agree with CD-MaN: Using "ORDER BY RAND()" will work nicely for small tables or when you do your SELECT only a few times.

    I also use the "num_value >= RAND() * ..." technique, and if I really want to have random results I have a special "random" column in the table that I update once a day or so. That single UPDATE run will take some time (especially because you'll have to have an index on that column), but it's much faster than creating random numbers for every row each time the select is run.

    0 讨论(0)
  • 2020-11-21 07:08

    For Firebird:

    Select FIRST 1 column from table ORDER BY RAND()
    
    0 讨论(0)
  • 2020-11-21 07:09

    Insted of using RAND(), as it is not encouraged, you may simply get max ID (=Max):

    SELECT MAX(ID) FROM TABLE;
    

    get a random between 1..Max (=My_Generated_Random)

    My_Generated_Random = rand_in_your_programming_lang_function(1..Max);
    

    and then run this SQL:

    SELECT ID FROM TABLE WHERE ID >= My_Generated_Random ORDER BY ID LIMIT 1
    

    Note that it will check for any rows which Ids are EQUAL or HIGHER than chosen value. It's also possible to hunt for the row down in the table, and get an equal or lower ID than the My_Generated_Random, then modify the query like this:

    SELECT ID FROM TABLE WHERE ID <= My_Generated_Random ORDER BY ID DESC LIMIT 1
    
    0 讨论(0)
  • 2020-11-21 07:09
    select r.id, r.name from table AS r
    INNER JOIN(select CEIL(RAND() * (select MAX(id) from table)) as id) as r1
    ON r.id >= r1.id ORDER BY r.id ASC LIMIT 1
    

    This will require a lesser computation time

    0 讨论(0)
提交回复
热议问题