我正在从 mysql 数据库中提取数据。我想添加多次跑步的长度,并按跑得最远的人的等级对它们进行排序。
function determineLength($db, $name){
$getLength = "select length from $db where name = $name and sex = 'male'";
$gettingLength = mysql_query($getLength);
while($gotLenth = mysql_fetch_array($gettingLength)){
more code that calculates total length and returns it as $lengthAccumulator
}
return array('totalLength'=>$lengthAccumulator);
}
现在,我有 30 个不同的男性,他们的名字永远不会改变,我需要对其进行分类。我应该如何在没有冗余的情况下为每个人运行 mysql 代码?我只能这样想——
$name = john;
$a = determineLength($db, $name);
$aLength = $a['totalLength'];
$name = sam;
$b = determineLength($db, $name);
$bLength = $b['totalLength'];
$name = henry;
$c = determineLength($db, $name);
$cLength = $c['totalLength'];
$name = ted;
$d = determineLength($db, $name);
$dLength = $d['totalLength'];
然后将 $aLength、$bLength、$cLength... 存储到一个数组中并以这种方式对它们进行排序。这似乎是错误的做法,更不用说冗余和缓慢了。数据库中有超过 40,000 行数据,因此尝试以这种方式执行此操作会在数据库中运行超过 120 万次?!我可以将名称数组传递给函数,然后在 mysql 代码中使用 ORDER BY length DESC 吗?
我需要帮助。谢谢你!
****the answer below by zane seemed to work to order the runners, but
我将如何使用这种方法呼应实际排名?我已将他的选择声明替换为我上面的声明,但我将如何回应一个人的等级?我可以将结果保存到数组中然后回显密钥吗?