php - 如何限制数据库调用的随机数为随机数加五?

标签 php mysql random

我需要一个随机数限制为 1-90(含)。所以无论我得到的随机数是什么,当前的随机数+ 5。

$query2 = $xxx->query("SELECT * FROM tablename ORDER BY tableid LIMIT FLOOR(RAND() * 90)  + 1, (FLOOR(RAND() * 90)  + 1) + 5");

最佳答案

没有理由调用 RAND() 两次。 如limit说:

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

LIMIT offset, max rows

所以你可以这样做

LIMIT FLOOR(RAND() * 90) + 1, 5

同时考虑到偏移量是从零开始的,您确定要 1-90 而不是 0-90 吗?

<小时/>

也可以生成数字 php-size

$rand = rand(1, 90);
// if your $xxx->query supports positional parameters
$query2 = $xxx->query('SELECT ... LIMIT ?,?', $rand, $rand + 5);
// or without it
$query2 = $xxx->query(sprintf('SELECT ... LIMIT %d,%d', $rand, $rand + 5));

关于php - 如何限制数据库调用的随机数为随机数加五?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29571845/

相关文章:

php - joomla BASE HREF 使用 HTTP 而不是 HTTPS

PHP parse_ini_file() 性能?

php - PDO 更新查询运行不会更改任何行。没有错误

mysql,从不同的表中选择(FULL OUTER JOIN 等效)?

php - 谷歌地方文本搜索 API 没有响应

mysql - 连接.sync({强制: false}) creates unwanted table

javascript - 如何每次使用随机毫秒数的setInterval?

algorithm - 在面板上放置随机的非重叠矩形

c# - 从 Azure 表存储中检索随机行?

php - 新的 MYSQL 服务器不知道 JSON 数据类型