1

我正在尝试运行 mysql 查询,该查询从排名从 5000 到 15000 的表中选择所有用户。这两个已放入变量中。让我把它改成代码,它会更好地解释它。

$user_rank = 10000 //this is actually a $row[''] pulled from a database
$rank_above = $user_rank + 5000;
$rank_below = $user_rank - 5000;

$fetch_users = mysql_query("SELECT * FROM users WHERE rank = '$rank_below' ?to? '$rank_above'");

但是,我还想让结果成为 LIMIT 1 个随机结果,以便它只从 users 表中选择一个随机用户。我是否以正确的方式去做这件事?非常感谢任何帮助,谢谢。

4

2 回答 2

2

您可以在 mysql 中使用BETWEEN子句

$sql = "SELECT * FROM users WHERE rank BETWEEN  5000 AND 15000"

例子:

$sql = "SELECT * FROM users WHERE rank BETWEEN %d AND %d"
$sql = sprintf($sql,(int)$rank_above,(int)$rank_below); // sanitize user input
于 2012-10-26T00:09:14.213 回答
1

试试这个:

SELECT * FROM users
WHERE rank BETWEEN $rank_below AND $rank_above
ORDER BY RAND()
LIMIT 1;
于 2012-10-26T00:10:49.580 回答