How to request a random row in SQL?

前端 未结 29 2943
孤城傲影
孤城傲影 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:10

    If possible, use stored statements to avoid the inefficiency of both indexes on RND() and creating a record number field.

    PREPARE RandomRecord FROM "SELECT * FROM table LIMIT ?,1";
    SET @n=FLOOR(RAND()*(SELECT COUNT(*) FROM table));
    EXECUTE RandomRecord USING @n;
    

提交回复
热议问题