繁体   English   中英

从mysql获取结果,然后从一行中检查指定值并计数

[英]fetching result from mysql and then check for specifc value from a row and count

agent表和lead_submission表。

 id_agent   agent_name    last_login
 1          xxx          2014-11-28 12:15:47
 2          abc          2014-12-06 12:51:45
 3          cvb          2014-12-12 12:51:45

id_agent      agent_name    entry_date           qa_details                 SelDisposiotn
  1            xxx        2014-11-28 12:15:47    504:Yes|581:|515:No|      Complete Survey
  1            xxx        2014-11-28 12:15:47    504:Yes|581:Yes|515:No|   Complete Survey
  2            abc        2014-12-06 12:51:45    504:Yes|522:|515:No|      Complete Survey
  3            cvb        2014-12-12 12:51:45    504:Yes|532:Yes|515:No|   Partial Survey

我的查询是从agent表中获取agent_name列表。 count(agent_name)lead_submissioncount(qa_details)lead_submissionSelDispositionlead_submissionleft join lead_submission on lead_submission.id_agent=agent.id_agent group by lead_submission.agent_name

<?php
//DB connection goes here

$sql="
SELECT a.agent_name LoginAgent
     , COUNT(s.agent_name) AgentApplicationHit
     , COUNT(s.qa_details) QADetails
     , s.selDisposition Status 
  FROM agent a
  LEFT
  JOIN lead_submission s
    ON s.id_agent = a.id_agent 
 WHERE s.entry_date BETWEEN '2014-11-28 12:15:47' AND '2014-12-06 12:51:45' 
   AND s.SelDisposition = 'Complete Survey' 
 GROUP 
    BY s.agent_name;
";

$query=mysql_query($sql);

?>   
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table border="1">
                <thead>
                  <tr>
                    <th>LoginAgent</th>
                    <th>AgentAppliccationHit</th>
                    <th>QA Details</th>
                    <th>Status</th>

                </tr>
  <?php while($row=mysql_fetch_assoc($query)) 
{?>

         <tr   style="color:red" >

        <td><?php echo $row['LoginAgent']; ?></td>
        <td><?php echo $row['AgentApplicationHit']; ?></td>  
        <td><?php echo $row['QADetails']; ?></td>   
        <td><?php echo $row['Status']; ?></td>          
        </tr>
                    <?php } ?>

我的结果:

 LoginAgent  AgentAppliccationHit          QA Details                Status
    xxx             2                        2                      Complete Survey
    abc             1                        2                      Complete Survey

到现在为止一切都很好。

现在我有另一个表question_details

qno  qshortcode   question
504   PPI         whatever
515   Test        whatever1

你可以看到这些QNO 504515question_details表是qa_detailslead_submission表,并在我的结果,其中我count(qa_details)

现在如何从lead_submission 504:Yes|522:|515:No|每个qa_details行中获取特定值计数504:Yes|522:|515:No| lead_submission表的每一行中lead_submission504515 ,例如

LoginAgent   AgentAppliccationHit      504(PPI)     515(Test)        Status
    xxx             2                    2            2            Complete Survey
    abc             1                    1            1            Complete Survey

首先,按照建议,您应该规范化数据库...但是...您可以使用当前设置获取计数...尽管您需要使用一些RAM ...如果有问题,座席,客户答案和排列很多。

编辑查询以在结果中还返回qa_details,例如:

SELECT qa_details, a.agent_name LoginAgent ... etc

开始:

 $stats_manager = array(); //or $stats_manager = [] ; depending on your php version.while($row=mysql_fetch_assoc($query)) 
        {    
                 //split up the answers into their respective questions

    foreach(explode('|', $row['qa_details']) as $Question)
        {

         $QnA = explode(':', $Question); //$QnA[0] gives you the $Question number, $QnA[1] is the answer
    $stats_manager[$row['LoginAgent']][$QnA[0]]++; //increment the number of times Agent has a question in his qa_details column
    $stats_manager[$row['LoginAgent']]['AgentApplicationHit'] = $row['AgentApplicationHit']; 
    $stats_manager[$row['LoginAgent']]['QADetails'] = $row['QADetails'];  //still necessary? I doubt.
    $stats_manager[$row['LoginAgent']]['Status'] = $row['Status']; 
    }
        }

现在,有了一个包含所有代理的多维数组,以及它们各自的Q和A的计数。然后,您可以简单地循环该数组(我省去了表头和html的其他非关键位

foreach($stats_manager as $agent_name =>  $stats_data)
{
echo "<tr><td>$agent_name</td>";
//loop hrough all the fields
foreach($stats_data as $key => $value){
echo "<td>$value</td>";
}
echo "</tr>";
}

关键是要避免在整理数据之前回显任何内容。 此外,尽管这将起作用,但最好进行归一化

未经测试。 应该管用。

编辑: $stats_manager之后,我每次循环都清空$stats_manager 已修复,因此我无法编辑更正后的代码,因此我删除了stats_manager变量的初始化,但是代码仍然可以正常工作,并带有“注意未定义变量”警告

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM