选择SQL上方和下方的行

时间:2014-01-12 14:23:46

标签: mysql sql

我正在为竞赛水手做一点申请,但我有一个问题。我需要来自特定水手的行上方2行和2行的数据。我的数据库如下所示:

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

3 个答案:

答案 0 :(得分:1)

您的第一个查询会以相反的顺序提取正确的结果。使用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));

答案 1 :(得分: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");

答案 2 :(得分: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)