2010-05-15 44 views
0

如何訂購此結果?從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>"; 
} 
+0

請使用「101010」-Icon格式化您的代碼。 – Simon 2010-05-15 07:57:06

+0

是不是應該是ORDER BY? – 2010-05-15 07:59:07

回答

0

使用 「ORDER BY」 - 子句:

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

這一點,從您的訂購結果大值。 使用"order by rank desc"以後代方向排序。 (大 - >小)

+0

感謝,但我怎麼能在中間顯示的搜索查詢和上面休息和下面的例子是: SEACH是馬修 和結果應該是下面顯示: - 約翰 - 26 喬 - 25 斯圖爾特 - 27 馬修 - 25 kelly - 24 brandon -23 magy - 22 .......等等。 – mathew 2010-05-15 08:31:45

+0

使用2個選擇子句:查找「等級 - 範圍」和「等級」之間的值,另一個查找「等級+ 1」和「等級+範圍」之間的值。以升序排列第一,以降序排列第二。 – Simon 2010-05-15 08:52:03

+0

OOPS我不好,我得到了答案。實際上它比這更容易。 唯一需要做的就是 「select * from $ where $ min $ max order by rank desc limit $ limit」 那就是... – mathew 2010-05-15 09:19:07

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

使用+1。使其可讀。 – egrunin 2010-05-15 08:01:29