找出2个阵列之间的差值

时间:2019-01-25 22:07:53

标签: php mysql arrays

我有2条搜索查询-一个将显示最近7天的内容。另一个将显示2周前的内容。 两者都很好。但是我想从第一个查询中获取结果,并从第二个查询中获取差异。然后显示带有差异的第一个查询。

$result_account = $db->query("
SELECT nid
     , COUNT(cat) AS qty
     , dte
     , descript
     , cat
     , name
     , user 
  FROM client_note AS cn 
  JOIN client_note_tag_items AS cnti 
    ON cnti.note_id = cn.nid 
  JOIN client_note_tags AS cnt 
    ON cnt.tag_id = cnti.tag_id 
 WHERE dte >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) 
   AND name NOT LIKE 'Resolution%' 
 GROUP 
    BY cat 
 ORDER 
    BY qty DESC 
 LIMIT 5
");
       if($count_account = $result_account->num_rows) {
               while($row = $result_account->fetch_object()){

          echo "<tr>";
          echo "<td><h6>".$row->cat."</h6></td><td><h3 class='text-primary'>".$row->qty."</h3></td>";
          echo "</tr>";
          }
       }

$result_previous = $db->query("SELECT nid, COUNT(cat) AS qty, dte, descript, cat, name, user FROM client_note AS cn JOIN client_note_tag_items AS cnti ON cnti.note_id = cn.nid JOIN client_note_tags AS cnt ON cnt.tag_id = cnti.tag_id WHERE (dte BETWEEN DATE_SUB(CURDATE(), INTERVAL 21 DAY) AND DATE_SUB(CURDATE(), INTERVAL 14 DAY)) AND name NOT LIKE 'Resolution%' GROUP BY cat ORDER BY qty DESC LIMIT 5");
    if($count_previous = $result_previous->num_rows) {
            while($row_p = $result_previous->fetch_object()){

          echo "<tr>";  
          echo "<td><h6>".$row_p->cat."</h6></td><td><h3 class='text-primary'>".$row_p->qty."</h3></td>";
          echo "</tr>";


            }
    }

第一个查询将导致:

Category   - Qty
Baseball   - 45
Football   - 33
Soccer     - 21
Hockey     - 7
Basketball - 3

第二个查询将导致:

Category   - Qty
Basketball - 38
Soccer     - 28
Hockey     - 16
Football   - 12
Baseball   - 12

现在我要这样显示

Category   - Qty Difference
Baseball   - 45  +33
Football   - 33  +21
Soccer     - 21  -7
Hockey     - 7   -9
Basketball - 3   -35

4 个答案:

答案 0 :(得分:2)

使用条件聚合,也可以在单个SQL查询中比较不同时间段的相同数据:

SELECT 
    cat, 
    SUM(IF(dte >= d.start1, 1, 0)) AS qty, 
    SUM(IF(dte >= d.start1, 1, 0)) - SUM(IF(dte < d.end2, 1, 0)) AS Difference, 
FROM 
    (SELECT DATE_SUB(CURDATE(), INTERVAL 7 DAY) start1, DATE_SUB(CURDATE(), INTERVAL 14 DAY) end2) as d
    CROSS JOIN client_note AS cn 
    JOIN client_note_tag_items AS cnti ON cnti.note_id = cn.nid 
    JOIN client_note_tags AS cnt ON cnt.tag_id = cnti.tag_id 
WHERE
    dte >= DATE_SUB(CURDATE(), INTERVAL 21 DAY) 
    AND name NOT LIKE 'Resolution%' 
GROUP BY cat 
ORDER BY qty DESC 
LIMIT 5

注意:

  • 第一个子查询只是避免重复输入相同的DATE_SUB...表达式的快捷方式
  • 我删除了输出中未使用的列
  • 您需要适当地对查询中的列进行别名;实际上,很难分辨出哪个列属于哪个表
  • 建议(在非古旧的MySQL版本中是强制性的)将所有个非聚合的列放在GROUP BY子句中
  • 未提供示例数据=>无法测试查询

PS:正如cornel.raiu所评论的,这种方法仅在您不需要在组合它们之前需要分别输出结果的情况下才有意义(否则,您最终将运行3个SQL查询,可能不是最佳选择。

答案 1 :(得分:0)

将第一组数字存储在两个关联数组中,然后在第二个循环中计算差异

$initial = [];
$diff = [];
$result_account = $db->query("SELECT nid, COUNT(cat) AS qty, dte, descript, cat, name, user FROM client_note AS cn JOIN client_note_tag_items AS cnti ON cnti.note_id = cn.nid JOIN client_note_tags AS cnt ON cnt.tag_id = cnti.tag_id WHERE `dte` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND name NOT LIKE 'Resolution%' GROUP BY cat ORDER BY qty DESC LIMIT 5");
   if($count_account = $result_account->num_rows) {
           while($row = $result_account->fetch_object()){

      echo "<tr>";
      echo "<td><h6>".$row->cat."</h6></td><td><h3 class='text-primary'>".$row->qty."</h3></td>";
      echo "</tr>";
      $initial[$row->cat] = $row->qty; //remember 1st results
      $diff[$row->cat] = $row->qty; //to be used
      }
   }

$result_previous = $db->query("SELECT nid, COUNT(cat) AS qty, dte, descript, cat, name, user FROM client_note AS cn JOIN client_note_tag_items AS cnti ON cnti.note_id = cn.nid JOIN client_note_tags AS cnt ON cnt.tag_id = cnti.tag_id WHERE (dte BETWEEN DATE_SUB(CURDATE(), INTERVAL 21 DAY) AND DATE_SUB(CURDATE(), INTERVAL 14 DAY)) AND name NOT LIKE 'Resolution%' GROUP BY cat ORDER BY qty DESC LIMIT 5");
if($count_previous = $result_previous->num_rows) {
        while($row_p = $result_previous->fetch_object()){

      echo "<tr>";  
      echo "<td><h6>".$row_p->cat."</h6></td><td><h3 class='text-primary'>".$row_p->qty."</h3></td>";
      echo "</tr>";
      $diff[$row_p->cat] -= $row_p->qty; 

        }
}
//now print the initial qty and the difference
$cats = array_keys($diff);
for($i=0; $i<sizeof($cats); $i++){
    echo "<tr>";
    echo "<td><h6>".$cats[$i]."</h6></td>";
    $first = $initial[$cats[$i]];
    echo "<td><h3 class='text-primary'>$first</h3></td>"
    $d = $diff[$cats[$i]];
    $sign = $d < 0 ? "-" : "+";
    echo "<td><h3 class='text-primary'>$sign $d</h3></td>";
    echo "<tr>";
}

答案 2 :(得分:0)

我要做的是创建另一个数组,该数组在键-值的基础上保存第一个查询的值,而在下一个查询中,仅从数组中相应的条目中减去值。

我认为通过使用2个查询,这是最简单,最快的方法。

注意:我没有测试此代码。

注2:我假设第一类运动缺失将计为0

注3:我编写的代码仅用于显示计算差异的方法。让我知道是否应该更新它,以提供您在那里所需的完全相同的输出。

void foo(volatile ControlReg *reg)
{
    reg -> opts_a = kOptsA_B;
}

foo:
        movs    r2, #1
        ldrh    r3, [r0]
        bfi     r3, r2, #9, #3
        strh    r3, [r0]        @ movhi
        bx      lr

更新-显示第一周及其旁边的差异

您可以尝试一下。我摆脱了foreach,仅使用查询循环来完成所有操作。

注意:再次,这是未经测试的代码

$results = array();

$result_account = $db->query("SELECT nid, COUNT(cat) AS qty, dte, descript, cat, name, user FROM client_note AS cn JOIN client_note_tag_items AS cnti ON cnti.note_id = cn.nid JOIN client_note_tags AS cnt ON cnt.tag_id = cnti.tag_id WHERE `dte` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND name NOT LIKE 'Resolution%' GROUP BY cat ORDER BY qty DESC LIMIT 5");
   if($count_account = $result_account->num_rows) {
      while($row = $result_account->fetch_object()){

        //output this query results here
        $results[$row->cat] = $row->qty;
      }
   }

$result_previous = $db->query("SELECT nid, COUNT(cat) AS qty, dte, descript, cat, name, user FROM client_note AS cn JOIN client_note_tag_items AS cnti ON cnti.note_id = cn.nid JOIN client_note_tags AS cnt ON cnt.tag_id = cnti.tag_id WHERE (dte BETWEEN DATE_SUB(CURDATE(), INTERVAL 21 DAY) AND DATE_SUB(CURDATE(), INTERVAL 14 DAY)) AND name NOT LIKE 'Resolution%' GROUP BY cat ORDER BY qty DESC LIMIT 5");
if($count_previous = $result_previous->num_rows) {
    while($row_p = $result_previous->fetch_object()){

      //output this query results here
      $results[$row_p->cat] = ((isset($results[$row_p->cat])) ? $results[$row_p->cat] : 0 ) - $row_p->qty;
    }
}


foreach( $results as $key => $result) {
  echo "<tr>";  
  echo "<td><h6>".$key."</h6></td><td><h3 class='text-primary'>".$result."</h3></td>";
  echo "</tr>";
}

答案 3 :(得分:0)

您可以做到

$result_account = $db->query("
SELECT nid
     , COUNT(cat) AS qty
     , dte
     , descript
     , cat
     , name
     , user 
  FROM client_note AS cn 
  JOIN client_note_tag_items AS cnti 
    ON cnti.note_id = cn.nid 
  JOIN client_note_tags AS cnt 
    ON cnt.tag_id = cnti.tag_id 
 WHERE dte >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) 
   AND name NOT LIKE 'Resolution%' 
 GROUP 
    BY cat 
 ORDER 
    BY qty DESC 
 LIMIT 5
");

if($count_account = $result_account->num_rows) {
    while($row = $result_account->fetch_object()){
        $$key[$row->cat]= $row->qty;
    }
}

$result_previous = $db->query("SELECT nid, COUNT(cat) AS qty, dte, descript, cat, name, user FROM client_note AS cn JOIN client_note_tag_items AS cnti ON cnti.note_id = cn.nid JOIN client_note_tags AS cnt ON cnt.tag_id = cnti.tag_id WHERE (dte BETWEEN DATE_SUB(CURDATE(), INTERVAL 21 DAY) AND DATE_SUB(CURDATE(), INTERVAL 14 DAY)) AND name NOT LIKE 'Resolution%' GROUP BY cat ORDER BY qty DESC LIMIT 5");
if($count_previous = $result_previous->num_rows) {
    while($row_p = $result_previous->fetch_object()){
        $second_array[$row_p->cat]= $row_p->qty;
    }
}

foreach ($first_array as $key => $value) {
    $difference_array[$key]=$value - $second_array[$key];
}

foreach ($difference_array as $key => $value){
    echo "<tr>";
    echo "<td><h6>".$key."</h6></td><td><h3 class='text-primary'>".$value."</h3></td>";
    echo "</tr>";
}