【问题标题】:How to sum data from the same table with different condition?如何对不同条件下同一张表中的数据求和?
【发布时间】:2017-08-17 15:54:09
【问题描述】:

我从同一个表名中获取了一些数据,但条件不同,我查询了 3 次,结果是:

101=>1
301=>1
501=>2
502=>4
---------------
101=>2
501=>1
---------------
101=>1
501=>1

第一列是教室,第二列是价值观。将这些值与同一教室相加的最佳方法是什么,结果将是:

101 = 4
301 = 1
501 = 4
502 = 4

我的查询命令:

$query = $db->prepare("SELECT COUNT(std_id) AS total, std_class  FROM attendance WHERE att_mode IN('mode-01','mode-04') AND att_attend='0' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class");
    $query->execute();
    while ($char = $query->fetch(PDO::FETCH_OBJ)) {
        $labels2[] = $char->std_class.'-'.$char->total;
        $link[] = $char->std_class;
    }
$query = $db->prepare("SELECT COUNT(std_id) AS total_sick,std_class FROM attendance WHERE att_mode ='mode-01' AND att_attend='2' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class");
    $query->execute();
    while($char = $query->fetch(PDO::FETCH_OBJ)){
        $sickCount[] = $char->std_class.'-'.$char->total_sick;
    }
$query = $db->prepare("SELECT COUNT(std_id) AS total_leave,std_class FROM attendance WHERE att_mode ='mode-01' AND att_attend='3' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class");
    $query->execute();
    while($char = $query->fetch(PDO::FETCH_OBJ)){
        $leaveCount[] = $char->std_class.'-'.$char->total_leave;
    }

【问题讨论】:

    标签: php sum


    【解决方案1】:

    使用子查询试试这个。

    "SELECT std_class , COUNT(std_id) as total ,(SELECT COUNT(std_id) AS total_sick FROM attendance WHERE att_mode ='mode-01' AND att_attend='2' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class ) AS total_sick    , (SELECT COUNT(std_id) FROM attendance WHERE att_mode ='mode-01' AND att_attend='3' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class) AS total_leave FROM attendance WHERE att_mode IN('mode-01','mode-04') AND att_attend='0' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class "
    

    如果不工作,请告诉我...

    【讨论】:

    • 我收到了这个错误:SQLSTATE[21000]: Cardinality violation: 1242 Subquery return more than 1 row
    【解决方案2】:

    编辑:Priyank 的答案显然更好,因为它是通过 SQL 完成的,所以没有 PHP 开销。

    我会获取每个查询的结果,将其粘贴到关联数组中,然后循环它们。它看起来像这样:

    $result = db_query1();
    $total = array();
    foreach ($result as $classroom => $value) {
        $total[$classroom] += $value;
    }
    
    $result = db_query2();
    foreach ($result as $classroom => $value) {
        $total[$classroom] += $value;
    }
    
    $result = db_query3();
    foreach ($result as $classroom => $value) {
        $total[$classroom] += $value;
    }
    
    print_r($total);
    

    【讨论】:

      【解决方案3】:

      如前所述,Priyank 的答案应该是要走的路,但是因为在看到其他答案之前,我为你写了一个小函数,所以无论如何我都会发布它,因为如果你不想要它可能会很有用任何理由都可以在 SQL 中完成。

      function mergeAndSum() {
          // get all the given arguments and put them into an array
          $arrays = func_get_args();
      
          // create an array that will contain the output
          $output = array();
      
          // loop through all the arguments
          foreach($arrays as $array) {
              // if an argument is not an array it will be skipped
              if(!is_array($array)) {
                  continue;
              }
              // otherwise it will be iterated
              foreach($array as $key => $value) {
                  // if the key is already in the $output array
                  if(array_key_exists($key,$output)) {
                      // add the value to the existing onve for that key
                      $output[$key] += $value;
                  } else {
                      // else store in the $output array the key with it's value
                      $output[$key] = $value;
                  }
      
              }
      
          }
      
          return $output;
      
      }
      

      所以,假设您有三个包含查询结果的数组

      $link = array(
          101=>1,
          301=>1,
          501=>2,
          502=>4,
      );
      $sickCount = array(
          101=>2,
          501=>1
      );
      $leaveCount = array(
          101=>1,
          501=>1
      );
      

      你只需将它们传入

      $output = mergeAndSum($link,$sickCount,$leaveCount);
      

      你的 $output 会如你所愿

      (
          101 => 4
          301 => 1
          501 => 4
          502 => 4
      )
      

      请记住,SQL 方式会更好,此函数足够灵活,可用于其他类似情况,即您需要合并和求和一些数组

      【讨论】:

        【解决方案4】:

        我终于找到了解决办法。我没有进行 3 次查询,而是只运行一个查询。 工作查询是:

        $query = $db->prepare("SELECT COUNT(std_id) AS total, std_class FROM attendance WHERE att_mode IN('mode-01','mode-04') AND att_attend IN('0','2','3') AND att_date=CURDATE() GROUP BY std_class ORDER BY std_class");
        

        “WHERE”子句将满足包含“0”、“2”和“3”值的“att_mode”。然后我得到了想要的结果。 感谢您之前的所有回答。

        【讨论】:

          猜你喜欢
          • 2019-11-21
          • 1970-01-01
          • 2021-07-19
          • 1970-01-01
          • 1970-01-01
          • 2019-11-09
          • 2014-12-11
          • 1970-01-01
          • 2020-04-10
          相关资源
          最近更新 更多