PHP比较两个while循环的结果


PHP compare the results of two while loops

我有以下查询:

$state = get['state'];
$getPossibleIDs = "SELECT * FROM ".$report_group_lookup;
     $qPossibleID = $conn->prepare($getPossibleIDs);
          $qPossibleID -> execute();
               while($rowPossible = $qPossibleID->fetch())
                    {
                         $possibleID = $rowPossible['id'];
                         $possibleName = $rowPossible['name'];
$getSpecificIDs = "SELECT * FROM rbs_report_type_2_specific WHERE rbs_report_type_id =".$state;
     $qSpecifcID = $conn -> prepare($getSpecificIDs);
          $qSpecifcID -> execute();
               while($rowSpecfics = $qSpecifcID->fetch())
                    {
                         $specificIDs = $rowSpecfics['rbs_specific_id'];
                         if($possibleID == $specificIDs)
                              {
                                   echo $possibleName."-Yes<br/>";
                              }
                         else
                              {
                                   echo $possibleName."-No<br/>";
                              }
                    }
}

我要做的是比较两个while查找的结果,第一个查找,getPossibleIDs,找到语句可以查看的所有可能的id。第二个是getSpecificIDs,它只查找之前应该被选中的那些。

它几乎可以工作,但是第二个查询查看具有以下信息的表:

------------------------------------
|rbs_report_type_id|rbs_specific_id|
| 1                | 1             |
| 1                | 2             |
| 1                | 3             |

所以我得到的结果重复了三次,每个rbs_spefic_id:

一次
HDD Coordinator-No
HDD Coordinator-No
HDD Coordinator-No
Rig Manager-Yes
Rig Manager-No
Rig Manager-No
Driller-No
Driller-Yes
Driller-No
Tank Hand-No
Tank Hand-No
Tank Hand-Yes

代替:

HDD Coordinator-No
Rig Manager-Yes
Driller-Yes
Tank Hand-Yes

有什么建议我可以改变输出吗?

谢谢

不使用内部循环,而是使用JOIN查询在第一个查询

中完成所有操作。
$getPossibleIDs = "
    SELECT rgl.id, rgl.name, 
    CASE WHEN rrt2s.rbs_specific_id IS NOT NULL THEN 'Yes' ELSE 'NO' END as `match`
    FROM ".$report_group_lookup." rgl 
    LEFT JOIN rbs_report_type_2_specific rrt2s 
         ON rrt2s.rbs_specific_id = rgl.id 
         AND rrt2s.rbs_report_type_id = ?";
$qPossibleID = $conn->prepare($getPossibleIDs);
$qPossibleID -> execute(array($state));
while($rowPossible = $qPossibleID->fetch())
     {
      echo $rowPossible['name']. " -".$rowPossible['match']."<br />";
     }

如果你只寻找第一个值,那么也许你可以完全避免使用第二个while循环。你可以这样做:

$getPossibleIDs = "SELECT * FROM ".$report_group_lookup;
$qPossibleID = $conn->prepare($getPossibleIDs);
$qPossibleID -> execute();
while($rowPossible = $qPossibleID->fetch()) {
    $possibleID = $rowPossible['id'];
    $possibleName = $rowPossible['name'];
    $getSpecificIDs = "SELECT * FROM rbs_report_type_2_specific WHERE rbs_report_type_id =".$state;
    $qSpecificID = $conn -> prepare($getSpecificIDs);
    $qSpecificID -> execute();
    $rowSpecifics = $qSpecificID->fetch()
    if ($rowSpecifics)   {                      //if a value is present then continue execution
        $specificIDs = $rowSpecifics['rbs_specific_id'];
        if ($possibleID == $specificIDs) {
            echo $possibleName."-match<br/>";
        } else {
            echo $possibleName."-No<br/>";
        }
    }
}

这将检查在第二次查找中是否存在一个值。如果是,那么它将执行您的逻辑,但只执行一次。