两次计算相同的字段

时间:2017-08-09 15:49:28

标签: php mysql

我正在尝试两次同一字段,因为当前设置输入工作代码为' 10'' 11'' 12',&# 39; 13',' 40',' 400',' 80'作为已完成的工作,但其他工作代码如55,56,57表示工作尚未完成。

通过以下我已设法计算work_code表并显示已完成的成功作业的数量,但我正在努力研究如何为未完成的作业(即55,56等)计算同一个表作为显示那也是。

以下是我的代码.....

<?php 
require 'db.php';

    $sqltran = mysqli_query($con, "SELECT *, ROUND(SUM(Sales),0), ROUND(AVG(Sales),2), COUNT(Work_Code) FROM results_tbl_1 WHERE RECORD_ID BETWEEN 468100 AND 500000 AND Work_Code IN ('10', '11', '12', '13', '40', '400', '80')  GROUP BY ENG_ID ORDER BY ROUND(SUM(Sales),0) DESC ") or die(mysqli_error($con));

    $arrVal = array();

    $i=1;
    while ($rowList = mysqli_fetch_array($sqltran)) {

                    $name = array(
                            'num' => '<img alt="" height="16" src="./top10.png" width="16">' . $i,


                            'eng'=> $rowList['ENG_ID'],

                            'totalvisits'=> $rowList['COUNT(Work_Code)'],

                            'ajv'=> '£' . $rowList['ROUND(AVG(Sales),2)'],

                            'sales'=> '£' . $rowList['ROUND(SUM(Sales),0)'],





                        );      


                        array_push($arrVal, $name); 
        $i++;           
    }
         echo  json_encode($arrVal);        


    mysqli_close($con);

然后数据显示在带有以下配置的引导程序表中。

<script type="text/javascript">

 var $table = $('#table');
         $table.bootstrapTable({
              url: 'test/board.php',
              search: false,
              pagination: true,
              buttonsClass: 'primary',
              showFooter: false,
              minimumCountColumns: 2,
              columns: [{
                  field: 'num',
                  title: 'Rank',
                  sortable: false,
              },{
                  field: 'eng',
                  title: 'Engineer',
                  sortable: false,
              },{
                  field: 'totalvisits',
                  title: 'Total Jobs',
                  sortable: false,
              },{
                  field: 'ajv',
                  title: 'AJV',
                  sortable: false,
              },{


                  field: 'sales',
                  title: 'Total',
                  sortable: false,
              },  ],

         });

我的问题是如何计算工作代码55,56等并在桌面上显示计数?

谢谢:)

5 个答案:

答案 0 :(得分:1)

创建两个新阵列,一个用于表示完成的作业代码,另一个用于表示未完成的作业代码。此外,在这种情况下,请勿尝试在查询中进行计数。

然后,当您循环查看结果时,请测试$row['Work_Code'] against the arrays using array_search()`。您还需要两个变量来存储计数。

它看起来像这样:

$completedJobs = array(1,2,3,4,5);
$incompleteJobs = array(7,8,9,10,11);
$completeTotal = 0;
$incompleteTotal = 0;
$i=1;
    while ($rowList = mysqli_fetch_array($sqltran)) {
      //I've stripped out what is not relevant to my example
      if(array_search($rowList['Work_Code'], $completedJobs) {
           $completeTotal++;                        
     }
     if(array_search($rowList['Work_code'],$incompleteJobs){
           $incompleteTotal++;
     }

}

答案 1 :(得分:0)

只需运行2个单独的查询,IN子句中包含不同的值:

已完成的工作:

<?php 
require 'db.php';

$sql_complete = mysqli_query($con, "SELECT *, ROUND(SUM(Sales),0), ROUND(AVG(Sales),2), COUNT(Work_Code) FROM results_tbl_1 WHERE RECORD_ID BETWEEN 468100 AND 500000 AND Work_Code IN ('10', '11', '12', '13', '40', '400', '80')  GROUP BY ENG_ID ORDER BY ROUND(SUM(Sales),0) DESC ") or die(mysqli_error($con));

while ($rowList = mysqli_fetch_array($sql_complete)) {
    // Outputs the number of completed jobs:
    echo $rowList['COUNT(Work_Code)'];
}
?>

不完整的工作:

<?php 

$sql_incomplete = mysqli_query($con, "SELECT *, ROUND(SUM(Sales),0), ROUND(AVG(Sales),2), COUNT(Work_Code) FROM results_tbl_1 WHERE RECORD_ID BETWEEN 468100 AND 500000 AND Work_Code IN ('55', '56', '57' )  GROUP BY ENG_ID ORDER BY ROUND(SUM(Sales),0) DESC ") or die(mysqli_error($con));

while ($rowList = mysqli_fetch_array($sql_incomplete)) {
    // Outputs the number of incomplete jobs:
    echo $rowList['COUNT(Work_Code)'];
}
?>

答案 2 :(得分:0)

假设所有代码不在&#34;已完成的工作&#34;代码列表不完整,查询可以像这样简单:

SELECT *, ROUND(SUM(IF(complete, Sales, null)),0), ROUND(AVG(IF(complete, Sales, null)),2), COUNT(IF(complete, 1, null)), COUNT(IF(complete, null, 1)) 
FROM ( SELECT *, Work_Code IN ('10', '11', '12', '13', '40', '400', '80') as complete
  FROM results_tbl_1 
  ) separated
WHERE RECORD_ID BETWEEN 468100 AND 500000 
GROUP BY ENG_ID 
ORDER BY ROUND(SUM(Sales),0) DESC

与问题无关的一些事情:

  • 永远不会在汇总查询中SELECT *,
  • 将别名用于聚合列

所以更好的查询版本是:

SELECT 
  ENG_ID, 
  ROUND(SUM(IF(complete, Sales, null)),0) as Total_sales_complete, 
  ROUND(AVG(IF(complete, Sales, null)),2) as Average_sales_complete, 
  COUNT(IF(complete, 1, null)) as Count_complete, 
  COUNT(IF(complete, null, 1)) as Count_incomplete
FROM ( SELECT *, Work_Code IN ('10', '11', '12', '13', '40', '400', '80') as complete
  FROM results_tbl_1 
  ) separated
WHERE RECORD_ID BETWEEN 468100 AND 500000 
GROUP BY ENG_ID 
ORDER BY Total_sales_complete DESC

修改

php代码中唯一的变化是使用别名:

$name = array(
 'num' => '<img alt="" height="16" src="./top10.png" width="16">' . $i,
 'eng'=> $rowList['ENG_ID'],
 'totalvisits'=> $rowList['Count_complete'],
 'ajv'=> '£' . $rowList['Total_sales_complete'],
 'sales'=> '£' . $rowList['Average_sales_complete'],

 'theSecondCount'=> $rowList['Count_incomplete'],
);    

在javascript端添加额外的列:

          {
              field: 'theSecondCount',
              title: 'Incomplete Jobs',
              sortable: false,
          }

作为旁注,标题&#34; Total Jobs&#34;有点误导,因为它显示完整作业的数量,以及&#34; Total&#34;暗示所有工作。

答案 3 :(得分:-2)

也许您想将UNION用于两个具有相应工作代码的SELECT。

答案 4 :(得分:-2)

我可能会错过显而易见但为什么不在“work_code in”子句中包含未完成的代码?