How to request a random row in SQL?

前端 未结 29 2997
孤城傲影
孤城傲影 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条回答
  •  猫巷女王i
    2020-11-21 07:14

    A simple and efficient way from http://akinas.com/pages/en/blog/mysql_random_row/

    SET @i = (SELECT FLOOR(RAND() * COUNT(*)) FROM table); PREPARE get_stmt FROM 'SELECT * FROM table LIMIT ?, 1'; EXECUTE get_stmt USING @i;
    

提交回复
热议问题