我通过查询从我的数据库中提取了一些行。假设结果是 120 行。我想按“分数”对这些行进行排序。这很容易,因为每一行都有一个名为“分数”的字段。接下来我想随机排序结果的前 20 行。所以:第一个按分数排序,然后随机排序前 20 个。我该怎么做?
编辑:要清楚:我想显示所有 120 行,只是随机显示前 20 行。
谢谢!
最佳答案
假设您已将所有结果保存在一个 PHP 数组 $rows
中,使用类似以下 SQL:
SELECT * from `table_name` order by `score`;
听起来你知道怎么做,所以我省略了细节。你想要的是以下内容:
// Get the first 20 rows
$top_twenty = array_slice($rows, 0, 20)
// Order them randomly
shuffle($top_twenty);
// Put them back into the $rows array, with their new order
$rows = array_replace($rows, $top_twenty);
关于php - MySQL 排序方式,然后对选择进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161568/