2013-10-31 32 views
-1

while循環內的多個查詢確實減慢了腳本..實際上我想要的是工程師列表假設它是20 ..特定工程師已經完成了多少修復。 。 我的代碼如下while while循環放慢腳本內的多個查詢

<?php 
    while($row=mysql_fetch_row($total_repairs)){ 
     // print_r($row); 
     //echo "SELECT COUNT(CustomerFeed.technical) as tech_sat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.technical in (1,2) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."'"; 
     $engineer=$row[0]; 
     //$engineer=$row['empName']; 
     $location=$row['locationName']; 
     $repairs=mysql_query("select count(ackNo) as tot from tblRepairQueue where assignedTo='".$engineer."' and month(ClosedAt)='".$month."' and YEAR(ClosedAt)='".$year."'"); 
     $tot_repairs=mysql_fetch_array($repairs); 
     $feedback_rec=mysql_query("select count(callStatus) as feedback from ContactCenter where callStatus='Close' and assignedTo='".$engineer."' and month(ClosedAt)='".$month."' and YEAR(ClosedAt)='".$year."' "); 
     $tot_feedback=mysql_fetch_array($feedback_rec); 
     $techsat=mysql_query("SELECT COUNT(CustomerFeed.technical) as tech_sat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.technical in (1,2) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."' "); 
     $tech_sat=mysql_fetch_array($techsat); 
     $techdsat=mysql_query("SELECT COUNT(CustomerFeed.technical) as tech_dsat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.technical in (4,5) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."' "); 
     $tech_dsat=mysql_fetch_array($techdsat); 
     $profsat=mysql_query("SELECT COUNT(CustomerFeed.prof) as prof_sat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.prof in (1,2) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $prof_sat=mysql_fetch_array($profsat); 
     $profdsat=mysql_query("SELECT COUNT(CustomerFeed.prof) as prof_dsat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.prof in (4,5) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $prof_dsat=mysql_fetch_array($profdsat); 
     /*$ressat=mysql_query("SELECT COUNT(CustomerFeed.res) as res_sat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.res in (1,2) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."' "); 
     $res_sat=mysql_fetch_array($ressat); 
     $resdsat=mysql_query("SELECT COUNT(CustomerFeed.res) as res_dsat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.res in (4,5) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $res_dsat=mysql_fetch_array($resdsat);*/ 
     $qttysat=mysql_query("SELECT COUNT(CustomerFeed.qtty) as qtty_sat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.qtty in (1,2) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $qtty_sat=mysql_fetch_array($qttysat); 
     $qttydsat=mysql_query("SELECT COUNT(CustomerFeed.qtty) as qtty_dsat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.qtty in (4,5) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $qtty_dsat=mysql_fetch_array($qttydsat); 
     /* $timesat=mysql_query("SELECT COUNT(CustomerFeed.tim) as time_sat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.tim in (1,2) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $time_sat=mysql_fetch_array($timesat); 
     $timedsat=mysql_query("SELECT COUNT(CustomerFeed.tim) as time_dsat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.tim in (4,5) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $time_dsat=mysql_fetch_array($timedsat);*/ 
     $overallsat=mysql_query("SELECT COUNT(CustomerFeed.overall) as overall_sat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.overall in (1,2) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.callStatus='Close' and ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $overall_sat=mysql_fetch_array($overallsat); 
     $overalldsat=mysql_query("SELECT COUNT(CustomerFeed.overall) as overall_dsat FROM ContactCenter, CustomerFeed WHERE CustomerFeed.overall in (4,5) AND CustomerFeed.callId = ContactCenter.callId AND ContactCenter.assignedTo = '".$engineer."' and month(ContactCenter.ClosedAt)='".$month."' and YEAR(ContactCenter.ClosedAt)='".$year."'"); 
     $overall_dsat=mysql_fetch_array($overalldsat); 
    ?> 
     <tr align="center" bgcolor="#F8F8F8"> 
     <td align="left"><?=$engineer?></td> 
     <!--<td><?=$location?></td>--> 
     <td><? echo ($tot_repairs['tot']!='0') ? $tot_repairs['tot'] : "-";?></td> 
     <td><? echo ($tot_feedback['feedback']!='0') ? $tot_feedback['feedback'] : "-";?></td> 
     <td><? echo round(($tot_feedback['feedback']/$tot_repairs['tot'])*100);?></td> 

     <td><? echo ($tech_sat['tech_sat']!='0') ? $tech_sat['tech_sat'] : "-";?></td> 
     <td><? echo round(($tech_sat['tech_sat']/$tot_feedback['feedback'])*100); ?></td> 

     <td><? echo ($tech_dsat['tech_dsat']!='0') ? $tech_dsat['tech_dsat'] : "-";?></td> 
     <td><? echo round(($tech_dsat['tech_dsat']/$tot_feedback['feedback'])*100); ?></td> 

     <td><? echo ($prof_sat['prof_sat']!='0') ? $prof_sat['prof_sat'] : "-";?></td> 
     <td><? echo round(($prof_sat['prof_sat']/$tot_feedback['feedback'])*100); ?></td> 

     <td><? echo ($prof_dsat['prof_dsat']!='0') ? $prof_dsat['prof_dsat'] : "-";?></td> 
     <td><? echo round(($prof_dsat['prof_dsat']/$tot_feedback['feedback'])*100); ?></td> 

     <!-- <td><?=$res_sat['res_sat']?></td> 
     <td><? echo round(($res_sat['res_sat']/$tot_feedback['feedback'])*100); ?></td> 
     <td><?=$res_dsat['res_dsat']?></td> 
     <td><? echo round(($res_dsat['res_dsat']/$tot_feedback['feedback'])*100); ?></td>--> 


     <td><? echo ($qtty_sat['qtty_sat']!='0') ? $qtty_sat['qtty_sat'] : "-";?></td> 
     <td><? echo round(($qtty_sat['qtty_sat']/$tot_feedback['feedback'])*100); ?></td> 

     <td><? echo ($qtty_dsat['qtty_dsat']!='0') ? $qtty_dsat['qtty_dsat'] : "-";?></td> 
     <td><? echo round(($qtty_dsat['qtty_dsat']/$tot_feedback['feedback'])*100); ?></td> 

     <!--<td><?=$time_sat['time_sat']?></td> 
     <td><? echo round(($time_sat['time_sat']/$tot_feedback['feedback'])*100); ?></td> 
     <td><?=$time_dsat['time_dsat']?></td> 
     <td><? echo round(($time_dsat['time_dsat']/$tot_feedback['feedback'])*100); ?></td>--> 

     <td><? echo ($overall_sat['overall_sat']!='0') ? $overall_sat['overall_sat'] : "-";?></td> 
     <td><? echo round(($overall_sat['overall_sat']/$tot_feedback['feedback'])*100); ?></td> 

     <td><? echo ($overall_dsat['overall_dsat']!='0') ? $overall_dsat['overall_dsat'] : "-";?></td> 
     <td><? echo round(($overall_dsat['overall_dsat']/$tot_feedback['feedback'])*100); ?></td> 
     </tr> 
<?php 
    } 
+0

所有查詢相關的或者這一切有什麼不同? –

+0

由於每個查詢只是簡單地改變工程師變量,所以你可以在while循環之外運行每個查詢的一個版本,並且如果你找到了你的答案,使用'GROUP BY ContactCenter.assignedTo' – Anthony

+0

,如果你在這裏發佈它會是非常好的。 – mandza

回答

0

您可以使用MySQL API的多查詢功能在PHP。

<?php 
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$query = "SELECT CURRENT_USER();"; 
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5"; 

/* execute multi query */ 
if ($mysqli->multi_query($query)) { 
    do { 
     /* store first result set */ 
     if ($result = $mysqli->store_result()) { 
      while ($row = $result->fetch_row()) { 
       printf("%s\n", $row[0]); 
      } 
      $result->free(); 
     } 
     /* print divider */ 
     if ($mysqli->more_results()) { 
      printf("-----------------\n"); 
     } 
    } while ($mysqli->next_result()); 
} 

/* close connection */ 
$mysqli->close(); 
?> 
0

你應該在子查詢計數循環使用一個查詢...