douxiji8707 2019-02-27 02:31
浏览 80

PHP根据同一个表中另一列的值查询表列

enter image description hereI have a database I have to query using PHP. There are 3 different columns I'm dealing with. What I have to do require a handful of steps so I'll list the steps 1 by 1...... 1) I have to go through each value in the 'nid' column. However, some of the 'nids' have duplicate value. Therefore I have to choose the 'nid' with the highest 'vid' value. 2) Once I select the 'nid' with the highest 'vid' value I then have to get the value of the 'title' column that's in the same row of the highest 'vid'. For example if I have a 'vid' of 1253 I have to select the content that's in column title that corresponds with 'vid' 1253. I have a lot of the steps. However, I'm getting stuck on once I grab the highest vid, being able to grab the content in the title column. Below is my code

<?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

The $queryTitle line is where I'm getting stuck. This is where I want to get the content of title column but only the title that corresponds with the highest vid.

  • 写回答

1条回答 默认 最新

  • douyunhuan9886 2019-02-27 03:30
    关注

    Try write query as below

    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 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭