偶然的你
ORDER BY RAND()如您所知,大多数人推荐的解决方案无法扩展到大型表。SET @r := (SELECT FLOOR(RAND() * (SELECT COUNT(*) FROM mytable)));SET @sql := CONCAT('SELECT * FROM mytable LIMIT 1 OFFSET ', @r);PREPARE stmt1 FROM @sql;EXECUTE stmt1;我在《SQL反模式:避免数据库编程的陷阱》一书中介绍了该解决方案和其他解决方案。如果要使用PHP进行此操作,则可以执行以下操作(未测试):<?php $mysqli->begin_transaction();$result = $mysqli->query("SELECT COUNT(*) FROM mytable")$row = $result->fetch_row(); $count = $row[0]; $offset = mt_rand(0, $count);$result = $mysqli->query("SELECT * FROM mytable LIMIT 1 OFFSET $offset");...$mysqli->commit();