【问题标题】:How to search for a specific value within a SQL result?如何在 SQL 结果中搜索特定值?
【发布时间】:2014-01-04 20:14:45
【问题描述】:

我有一个具有以下结构和信息的 SQL 数据库:

idClub   Pts   GD
1        3      2
2        4      0
3        5     -1
4        0     -3
5        9      5
6        1      2
7        3      1
8        0     -2
9        6      0
10       7      5

如果这样做:

$result = $mysqli->query("SELECT * FROM table ORDER BY Pts Desc, GD 描述");

它将提供这个:

idClub   Pts   GD
5        9      5    //Row Number = 1
10       7      5    //Row Number = 2
9        6      0    //Row Number = 3
3        5     -1    //Row Number = 4
2        4      0    //Row Number = 5
1        3      2    //Row Number = 6
7        3      1    //Row Number = 7
6        1      2    //Row Number = 8
8        0     -2    //Row Number = 9
4        0     -3    //Row Number = 10

每个 idClub 都有自己的个人资料页面 - profile.php?Id=X - 我需要在其中显示带有 id 的俱乐部以及表格中上下两支球队。

有没有办法知道 $result 中给定值的行号(位置)? 例如,profile.php?Id=3 它将是 4;对于 profile.php?Id=1 它将是 6,依此类推..

例如,对于 profile.php?Id=3,我需要一个提供以下内容的 $result:

idClub   Pts   GD
10       7      5
9        6      0
3        5     -1
2        4      0
1        3      2

提前致谢。

【问题讨论】:

标签: php mysql sql mysqli


【解决方案1】:

首先,接受您当前的查询,

SELECT
  *
FROM
  atable
ORDER BY
  Pts DESC,
  GD DESC

并使用涉及变量赋值的众所周知的技术为其添加行编号:

SELECT
  atable.*,
  @row := @row + 1 AS row
FROM
  atable
CROSS JOIN
  (SELECT @row := 0) AS p
ORDER BY
  Pts DESC,
  GD DESC

这将根据指定的顺序为您的行分配排名。现在,如果我们知道与指定 idClub 关联的行号,我们只需将上述查询用作派生表,并根据条件过滤其结果集

WHERE
  row BETWEEN @idXrow - 2 AND @idXrow + 2

我们可以找出@idXrow的值。为此,我们可以在子查询中再添加一个变量赋值:

@idXrow := CASE idClub WHEN 3 THEN @row ELSE @idXrow END

其中3 是指定的idClub 值——在您的最终查询中,它可能需要替换为参数引用。无论如何,完成这项工作的complete query 看起来像这样:

SELECT
  row,
  idClub,
  Pts,
  GD
FROM
  (
    SELECT
      atable.*,
      @row := @row + 1 AS row,
      @idXrow := CASE idClub WHEN 3 THEN @row ELSE @idXrow END
    FROM
      atable
    CROSS JOIN
      (SELECT @row := 0) AS p
    ORDER BY
      Pts DESC,
      GD DESC
  ) AS s
WHERE
  row BETWEEN @idXrow - 2 AND @idXrow + 2
;

【讨论】:

    【解决方案2】:

    如果您只讨论$result 中的查询,您可以像这样跟踪在打印循环中迭代的变量:

    $result = $mysqli->query("SELECT * FROM table ORDER BY Pts Desc, GD Desc");
    $c = 0;
    
    echo "<table>\n<tr>\n<td>idClub</td>\n<td>Pts</td>\n<td>GD</td>\n<td>Row #</td>\n</tr>\n";
    
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr>\n<td>$row[idClub]</td>\n<td>$row[Pts]</td>\n<td>$row[GD]</td>\n<td>$c</td>\n</tr>\n";
        $c++;
    }
    
    echo "</table>";
    

    【讨论】:

    • 但是在这个“echo”中,将打印完整的表格(第 1 到第 10 行)。我需要的是(例如 profile.php?id=3)只打印第 2 到第 6 行。
    【解决方案3】:

    另一种可能的解决方案:

    $selClub = $_GET["Id"];
    $result = $mysqli->query("SELECT idClub FROM table");
    for ($i = 1; $i <= $result->num_rows; $i++) {
        if ( $result->fetch_object()->idClub == $selClub )
            $position = $i;
    }
    
    if ( $position <= 2 ) {
        $result = $mysqli->query("SELECT * FROM table ORDER BY Pts Desc, GD Desc LIMIT 0, 5");
    } elseif ( $position >= $result->num_rows - 1 ) {
        $result = $mysqli->query("SELECT * FROM table ORDER BY Pts Desc, GD Desc LIMIT ($result->num_rows-5), 5");
    } else {
        $result = $mysqli->query("SELECT * FROM table ORDER BY Pts Desc, GD Desc LIMIT ($position-3), 5");
    }
    

    第一个和第二个 $result 使用同名真的有问题吗?

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2021-12-02
      相关资源
      最近更新 更多