簡體   English   中英

PHP根據同一表中另一列的值查詢表列

[英]PHP Query a table column based on value from another column in same table

在此處輸入圖片說明 我有一個數據庫,必須使用PHP進行查詢。 我正在處理3個不同的列。 我需要做的步驟僅需幾個步驟,因此我將按步驟1列出所有步驟……1)我必須仔細檢查“ nid”列中的每個值。 但是,某些“小人物”具有重復的價值。 因此,我必須選擇“ vid”值最高的“ nid”。 2)一旦我選擇了具有最高“ vid”值的“ nid”,我就必須獲取最高“ vid”的同一行中“ title”列的值。 例如,如果我具有“ vid” 1253,則必須選擇與“ vid” 1253對應的列標題中的內容。我有很多步驟。 但是,一旦抓到最高的vid,便會陷入困境,因為它能夠抓取title列中的內容。 下面是我的代碼

<?php
    // Establish all database credential variables
    $serverName = "localhost";
    $username = "root";
    $password = "root";
    $databaseName = "redesign_static";
    // Create Database Connection
    $connection = new mysqli($serverName, $username, $password, $databaseName);
    // Check Database Connection
    if ($connection->connect_error) {
      die("Connection failed:" . $connection->connect_error);
    } // line ends if statement
    $queryNodeRevision = "SELECT nid, vid, title FROM node_revision";
    // line above creates variable $queryNodeRevision > selects column "nid" from table "node_revision"
    $results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
    // line above creates variable $results > actually queries that database and passes in variable "$queryNodeRevision"
    $storeNIDAndVIDValues = []; // empty array to store max 'vid' values

    for ($i = 0; $i < 8000; $i++) {
      $storeNIDAndVIDValues[$i] = 0;
      // line above assigns initial 'vid'; starts at 0
    }

    while ($row = mysqli_fetch_array($results)) {
      $currentNID = $row['nid'];
      // line above creates variable that represents the current 'nid' of row (aka the key)
      $currentVID = $row['vid'];
      // line above creates variable that represents the current value of the 'vid' (the number you want to compare)
    if ($currentVID > $storeNIDAndVIDValues[$currentNID]) {
        // if the value of'$currentVID' is greater than what's stored in array '$storeNIDAndVIDValues' at current nid position
        // $storeNIDAndVIDValues[$currentNID] = goes into array $storeNIDAndVIDValues and gets the value of nid key (in this case value represents what position the nid is at)
           $storeNIDAndVIDValues[$currentNID] = $currentVID;
           // line above > becomes max 'vid' at that time 
           $titleOfSelectedVID = $row['title'];
          // $row['title'] = gets the value of the current 'title'
        $queryTitle = "SELECT title FROM node_revision WHERE $currentVID ";
        // line above is query variable that targets 'title' column row that has highest 'vid' value
     } // line ends if statement
    } // line closes while loop
  ?>

$queryTitle = "SELECT title FROM node_revision WHERE

$ queryTitle行是我遇到的問題。 這是我要獲取title列的內容的地方,但僅獲取與最高vid對應的標題。

嘗試如下編寫查詢

SELECT a.* FROM node_revision as a INNER JOIN (SELECT MAX(vid) as vid, nid FROM node_revision GROUP BY nid) as b on a.nid = b.nid WHERE a.vid = b.vid 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM