2014-01-12 51 views
2

我正在爲競爭水手的一個小應用程序,但我有一個問題。我需要來自特定水手的上面2行和下面2行的數據。我的數據庫看起來是這樣的:選擇上面和下面的行SQL

enter image description here

這也是我用它來獲得上方和下方數據的SQL:

$data_above= mysql_query("SELECT * FROM Laser_Radiaal WHERE points < (SELECT points FROM Laser_Radiaal WHERE PLACE = ID) ORDER BY PLACE DESC LIMIT 2"); 

$data_under= mysql_query("SELECT * FROM Laser_Radiaal WHERE points < (SELECT points FROM Laser_Radiaal WHERE PLACE = ID) ORDER BY points ASC LIMIT 2"); 

但它不工作,做我錯了嗎?

謝謝!

編輯:我收到以下錯誤:

Warning: mysql_fetch_array() expects parameter 1 to be resource 
+1

好了,一旦情況下,你必須用''地方asc'和其他'點asc'秩序。我期望'點desc'和'點asc'。 –

+0

您好Gordon,感謝您的快速反應,asc/desc問題只是我嘗試使其工作時犯的一個小錯誤。但它仍然不起作用。 –

回答

0

你在做WHERE points < ...這兩種情況下,但在下面的情況下(或以上,這取決於你如何看待它),你不希望WHERE points > ...

所以,這樣的:

$data_above= mysql_query("SELECT * FROM Laser_Radiaal WHERE points < (SELECT points FROM Laser_Radiaal WHERE PLACE = ID) ORDER BY PLACE DESC LIMIT 2"); 

$data_under= mysql_query("SELECT * FROM Laser_Radiaal WHERE points > (SELECT points FROM Laser_Radiaal WHERE PLACE = ID) ORDER BY points ASC LIMIT 2"); 
+0

謝謝,我已經嘗試過了,但我仍然得到以下錯誤:警告:mysql_fetch_array()預計參數1是資源,鑑於布爾嗯 –

+0

,所以我不是在那個特定主題的專家,也許有人誰知道更多關於可以嵌入,但如果我可能建議可能而不是做'SELECT * FROM Laser_Radiaal',看看它是否更好,如果你從該表中選擇特定的列,即'SELECT ID FROM Laser_Radiaal'? – Scott

+0

媽的,它仍然簡化版,請與您的解決方案 –

1

你的第一個查詢拉正確的結果,但在反向order.Use array_reverse()扭轉結果的順序。

使用>=在第二查詢和LIMIT 3拉目標值與上述兩個。

使用array_merge合併兩個數組。

以下代碼(using PDO)說明了此。

//Prepare below query 
    $query1 ="SELECT * FROM Laser_Radiaal WHERE points < 
       (SELECT points FROM Laser_Radiaal WHERE PLACE = ?) 
        ORDER BY PLACE DESC LIMIT 2"; 
    $stmt1 = $dbh->prepare($query1); 
    // Assign parameter 
    $stmt1->bindParam(1,$ID); 
    //Execute query 
    $stmt1->execute(); 
    $result1 = $stmt1->fetchAll(); 
    $result1r = array_reverse($result1); 
    //Prepare abovequery including target 
    $query2 ="SELECT * FROM Laser_Radiaal WHERE points >= 
        (SELECT points FROM Laser_Radiaal WHERE PLACE = ?) 
        ORDER BY PLACE ASC LIMIT 3"; 
    $stmt2 = $dbh->prepare($query2); 
    // Assign parameter 
    $stmt2->bindParam(1,$ID); 
    //Execute query 
    $stmt2->execute(); 
    $result2 = $stmt2->fetchAll(); 
    $finalResult = array_merge(array_reverse($result1), $result2); 
    print_r(array_values($finalResult)); 
0

請檢查下面的例子中它會幫助你對這個

select * from games order by w; 
+--------+------+------+------+------+ 
| name | p | w | d | l | 
+--------+------+------+------+------+ 
| team b | 1 | 0 | 1 | 0 | 
| team a | 1 | 0 | 0 | 1 | 
| team c | 1 | 1 | 0 | 0 | 
| team d | 2 | 2 | 0 | 0 | 
+--------+------+------+------+------+ 
4 rows in set (0.00 sec) 


mysql> SELECT NAME,P,W,D,L,@curRank := @curRank + 1 as rank FROM (select NAME,P,W,D,L from games order by w DESC) x,(SELECT @curRank := 0) r; 
+--------+------+------+------+------+------+ 
| NAME | P | W | D | L | rank | 
+--------+------+------+------+------+------+ 
| team d | 2 | 2 | 0 | 0 | 1 | 
| team c | 1 | 1 | 0 | 0 | 2 | 
| team b | 1 | 0 | 1 | 0 | 3 | 
| team a | 1 | 0 | 0 | 1 | 4 | 
+--------+------+------+------+------+------+ 
4 rows in set (0.01 sec) 


mysql> SELECT NAME,P,W,D,L,rank FROM (SELECT NAME,P,W,D,L,@curRank := @curRank + 1 as rank,if(name ="team b",@curRank := @curRank*-1,0) AS required_rank FROM (select NAME,P,W,D,L from games order by w DESC) x,(SELECT @curRank := 0) r) p order by abs(rank) desc limit 3; 
+--------+------+------+------+------+------+ 
| NAME | P | W | D | L | rank | 
+--------+------+------+------+------+------+ 
| team b | 1 | 0 | 1 | 0 | 3 | 
| team c | 1 | 1 | 0 | 0 | 2 | 
| team a | 1 | 0 | 0 | 1 | -2 | 
+--------+------+------+------+------+------+ 
3 rows in set (0.00 sec) 
+0

在上面的例子中,我得到匹配的原始數據和它的上面和下面的記錄,就像改變限制,你可以得到要求的範圍 –