从MySQL中选择相似的值并对结果进行排序

时间:2010-05-15 07:54:19

标签: php mysql

如何订购此结果??

$range = 5;  // you'll be selecting around this range. 
$min = $rank - $range;
$max = $rank + $range;
$limit = 10; // max number of results you want. 

$result = mysql_query("select * from table where rank between $min and $max limit $limit");

while($row = mysql_fetch_array($result))
{
   echo $row['name']."&nbsp;-&nbsp;".$row['rank']."<br>";
}

2 个答案:

答案 0 :(得分:2)

$result = mysql_query(
    "select * from table where rank between $min and $max " . 
    "order by rank asc limit $limit"
);

答案 1 :(得分:0)

使用“order by” - 子句:

mysql_query("select * from table where rank between $min and $max order by rank limit $limit");

这会将您的结果从小到大的顺序排序。 使用"order by rank desc"以后代方向排序。 (大 - >小)