首页 > 解决方案 > MySQL - 将部分查询加入新查询?

问题描述

我有以下查询表格的代码。然后它使用结果进行另一个查询。然后使用该结果进行第三次查询。

但是如何从第二个查询中获取 userid 字段,以便从用户表中获取一个名称并将其连接到第三个查询的结果中?

请注意,一旦我弄清楚代码,我会将其转换为准备好的语句。在找出查询时,使用遗留代码对我来说更容易。

    $selectaudioid = "SELECT audioid FROM subscribe WHERE userid = $userid";
    $audioResult=$dblink->query($selectaudioid);
    
    if ($audioResult->num_rows>0)   {       
            while ($row = $audioResult->fetch_assoc())  {
                $newaudio = $row[audioid];  
                $getallaudio = "SELECT opid, userid from audioposts WHERE audioid = $newaudio"  ;
                $getallresult = $dblink->query($getallaudio);           
                
                if ($getallresult->num_rows>0)  {                       
                while ($row = $getallresult->fetch_assoc())  {
                    $opid = $row[opid];
                    $opuserid = $row[userid];
                    $getreplies = 
                        "SELECT * from audioposts ap WHERE opid = $opid AND opid                        
                         NOT IN (SELECT opid FROM audioposts WHERE audioposts.opid = '0' )";    
                    $getreplyresults = $dblink->query($getreplies);
                    
                    if ($getreplyresults->num_rows>0)   {
                    while ($row = $getreplyresults->fetch_assoc())  {
                        $dbdata[]=$row;
                }                           
            }                       
        }
    }
 }
}       "SELECT * from audioposts ap WHERE opid = $opid AND opid                        
                         NOT IN (SELECT opid FROM audioposts WHERE audioposts.opid = '0' )";    
                    $getreplyresults = $dblink->query($getreplies);
                    
                    if ($getreplyresults->num_rows>0)   {
                    while ($row = $getreplyresults->fetch_assoc())  {
                        $dbdata[]=$row;
                }                           
             }                      
           }
         }
       }
    } 
echo json_encode($dbdata);

我需要的结果是$getreplyresults由原始结果中的 $row[userid] 连接到每一行的 json 编码实例的行。

标签: phpmysqlinner-joinself-join

解决方案


这就是我最后所做的。现在我只需要弄清楚如何将其转换为准备好的语句以避免恶意注入。

    $selectaudioid = "SELECT audioid FROM subscribe WHERE userid = $userid";
    $audioResult=$dblink->query($selectaudioid);
    
        if ($audioResult->num_rows>0)   {       
            while ($row = $audioResult->fetch_assoc())  {
                $newaudio = $row[audioid];  
                $getallaudio = "
                SELECT ap.audioid, ap.title, us.name FROM audioposts ap                     
                INNER JOIN audioposts a2 ON a2.audioid = ap.opid
                INNER JOIN users us ON us.id = a2.userid 
                WHERE ap.opid = $newaudio AND ap.opid <> '0'
                ";
                
                $getallresult = $dblink->query($getallaudio);           
                
                if ($getallresult->num_rows>0)  {                       
                while ($row = $getallresult->fetch_assoc())  {
                    $dbdata[]=$row;                         
        }}}}
    

推荐阅读