MySQL使用WHERE语句定位特定行

时间:2019-02-27 15:09:22

标签: php mysql sql where

enter image description here我正在尝试查询一个关注3个不同列的表。我有列nid,vid和title。我必须遍历每个nid值,获取每个nid值中的最高vid,一旦获得最高vid,就必须获取该vid同一行中的标题。最终,我需要每个标题的列表。我有第一步,第二步。我需要帮助的唯一部分是获取正确的标题内容,因为它必须再次是与最高vid在同一行中的标题。我以为where语句将是执行此操作的好方法,但我陷入了困境。附件是表格的屏幕截图。

$queryNodeRevision = "SELECT nid, MAX(vid) as vid, title FROM node_revision WHERE title = vid GROUP BY nid";
    // line above creates variable $queryNodeRevision > 

    $results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
    // line above creates variable $results > actually queries that database and passes in variable "$queryNodeRevision"

    while ($row = mysqli_fetch_array($results)) {
      // line above creates while loop that loops through > $row = mysqli_fetch_array($results)
      // $row is variable that's set to mysqli_fetch_array (with variable $results being passed in)
      // mysqli_fetch_array > creates an associate array for each row in a table
      // $results > variable that's being passed into associative array that represents the variable that's quering "SELECT nid FROM node_revision";
      $currentNID = $row['nid'];
      // line above creates variable that represents the current 'nid' of row (aka the key)
      // $row['nid'] = gets the key # of the current 'nid'
      $currentVID = $row['vid'];
      // line above creates variable that represents the current value of the 'vid' (the number you want to compare)
      // $row['vid'] = gets the value of the current 'vid'  
      $theTitleIWant = $row['title'];
      // line above creates variable that represents the current value of the 'title'
      // $row['title'] = gets the value of the current 'title'  
      echo "<h1>" . $row['title'] . "</h1>";
      // line prints out desired 'title' into h1 tag
    } // line closes while loop

下面是我要用其完成代码的where语句所在的行。

$queryNodeRevision = "SELECT nid, MAX(vid) as vid, title FROM node_revision WHERE title = vid GROUP BY nid";[![enter image description here][1]][1]

1 个答案:

答案 0 :(得分:1)

请考虑运行您的汇总查询(不包括对 title 的任何引用),该查询将返回到单位级别表。下面为每个 nid 返回与最高 vid 相对应的 title

arr