gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:35:10 24 4
gpt4 key购买 nike

我需要一个随机数限制为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com