MySQL在JSON输出中运行总计

时间:2016-12-07 05:11:10

标签: mysql json running-total

已经看了很久,但无法克服这个问题。已找到运行的总问题,但没有人处理2个查询......

Array(
    [0] => Loganathan, logu@gmail.com
    [1] => Nathan, nathan@gmail.com
    [2] => Tester, tester@gmail.com
)

以上是我最接近的,但无法正确输出..

上面的当前输出是......(显然我有WHILE和FOREACH完全错误!!)

  $query = "SELECT d.DateAdded, @running_sum:=@running_sum + d.count AS running FROM (SELECT DateAdded, COUNT(*) AS 'count' FROM details WHERE MemberStatus = 'Active'GROUP BY DATE_FORMAT(DateAdded,'%Y-%m-%d') ORDER BY DateAdded ) d JOIN (SELECT @running_sum := 0 AS dummy) dummy;";
  $result = mysqli_query($mysqli,$query);

  $array = array();

  while ($row2 = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      $RunningTotal = $row2['running'];

   foreach($mysqli->query('SELECT DateAdded, COUNT(*) FROM details GROUP BY DATE_FORMAT(DateAdded,"%Y-%m-%d")') as $row) {  
        $year = date("Y-m-d",strtotime($row['DateAdded']));
        array_push($array,array('Year'=>$year, 'Numb'=>$row['COUNT(*)'],'Total'=>$RunningTotal));
        } 
    }


echo json_encode($array);

年:添加日期成员

Numb:有多少人在那个特定日期报名

总计:运行新注册总数

下面是它应该是什么样的..(只是整理格式化)

        [{"Year":"2016-10-27","Numb":"1","Total":"1
        "},{"Year":"2016-10-31","Numb":"1","Total":"1
        "},{"Year":"2016-11-02","Numb":"1","Total":"1
        "},{"Year":"2016-11-05","Numb":"1","Total":"1
        "},{"Year":"2016-11-07","Numb":"1","Total":"1
        "},{"Year":"2016-11-08","Numb":"1","Total":"1
        "},{"Year":"2016-11-09","Numb":"1","Total":"1
        "},{"Year":"2016-11-10","Numb":"2","Total":"1
        "},{"Year":"2016-11-16","Numb":"2","Total":"1
        "},{"Year":"2016-11-20","Numb":"1","Total":"1
        "},{"Year":"2016-11-22","Numb":"1","Total":"1
        "},{"Year":"2016-12-05","Numb":"1","Total":"1
        "},{"Year":"2016-12-06","Numb":"1","Total":"1
        "},{"Year":"2016-12-07","Numb":"1","Total":"1
        "},{"Year":"2016-10-27","Numb":"1","Total":"2
        "},{"Year":"2016-10-31","Numb":"1","Total":"2
        "},{"Year":"2016-11-02","Numb":"1","Total":"2
        "},{"Year":"2016-11-05","Numb":"1","Total":"2
        "},{"Year":"2016-11-07","Numb":"1","Total":"2
        "},{"Year":"2016-11-08","Numb":"1","Total":"2
        "},{"Year":"2016-11-09","Numb":"1","Total":"2
        "},{"Year":"2016-11-10","Numb":"2","Total":"2
        "},{"Year":"2016-11-16","Numb":"2","Total":"2
        "},{"Year":"2016-11-20","Numb":"1","Total":"2
        "},{"Year":"2016-11-22","Numb":"1","Total":"2
        "},{"Year":"2016-12-05","Numb":"1","Total":"2
        "},{"Year":"2016-12-06","Numb":"1","Total":"2
        "},{"Year":"2016-12-07","Numb":"1","Total":"2
        "},{"Year":"2016-10-27","Numb":"1","Total":"3
        "},{"Year":"2016-10-31","Numb":"1","Total":"3
        "},{"Year":"2016-11-02","Numb":"1","Total":"3
        "},{"Year":"2016-11-05","Numb":"1","Total":"3
        "},{"Year":"2016-11-07","Numb":"1","Total":"3
        "},{"Year":"2016-11-08","Numb":"1","Total":"3
        "},{"Year":"2016-11-09","Numb":"1","Total":"3
        "},{"Year":"2016-11-10","Numb":"2","Total":"3
        "},{"Year":"2016-11-16","Numb":"2","Total":"3
        "},{"Year":"2016-11-20","Numb":"1","Total":"3
        "},{"Year":"2016-11-22","Numb":"1","Total":"3
        "},{"Year":"2016-12-05","Numb":"1","Total":"3
        "},{"Year":"2016-12-06","Numb":"1","Total":"3
        "},{"Year":"2016-12-07","Numb":"1","Total":"3
        "},{"Year":"2016-10-27","Numb":"1","Total":"4
        "},{"Year":"2016-10-31","Numb":"1","Total":"4
        "},{"Year":"2016-11-02","Numb":"1","Total":"4
        "},{"Year":"2016-11-05","Numb":"1","Total":"4
        "},{"Year":"2016-11-07","Numb":"1","Total":"4
        "},{"Year":"2016-11-08","Numb":"1","Total":"4
        "},{"Year":"2016-11-09","Numb":"1","Total":"4
        "},{"Year":"2016-11-10","Numb":"2","Total":"4
        "},{"Year":"2016-11-16","Numb":"2","Total":"4
        "},{"Year":"2016-11-20","Numb":"1","Total":"4
        "},{"Year":"2016-11-22","Numb":"1","Total":"4
        "},{"Year":"2016-12-05","Numb":"1","Total":"4
        "},{"Year":"2016-12-06","Numb":"1","Total":"4
        "},{"Year":"2016-12-07","Numb":"1","Total":"4
        "},{"Year":"2016-10-27","Numb":"1","Total":"5
        "},{"Year":"2016-10-31","Numb":"1","Total":"5
        "},{"Year":"2016-11-02","Numb":"1","Total":"5
        "},{"Year":"2016-11-05","Numb":"1","Total":"5
        "},{"Year":"2016-11-07","Numb":"1","Total":"5
        "},{"Year":"2016-11-08","Numb":"1","Total":"5
        "},{"Year":"2016-11-09","Numb":"1","Total":"5
        "},{"Year":"2016-11-10","Numb":"2","Total":"5
        "},{"Year":"2016-11-16","Numb":"2","Total":"5
        "},{"Year":"2016-11-20","Numb":"1","Total":"5
        "},{"Year":"2016-11-22","Numb":"1","Total":"5
        "},{"Year":"2016-12-05","Numb":"1","Total":"5
        "},{"Year":"2016-12-06","Numb":"1","Total":"5
        "},{"Year":"2016-12-07","Numb":"1","Total":"5
        "},{"Year":"2016-10-27","Numb":"1","Total":"6
        "},{"Year":"2016-10-31","Numb":"1","Total":"6
        "},{"Year":"2016-11-02","Numb":"1","Total":"6
        "},{"Year":"2016-11-05","Numb":"1","Total":"6
        "},{"Year":"2016-11-07","Numb":"1","Total":"6
        "},{"Year":"2016-11-08","Numb":"1","Total":"6
        "},{"Year":"2016-11-09","Numb":"1","Total":"6
        "},{"Year":"2016-11-10","Numb":"2","Total":"6
        "},{"Year":"2016-11-16","Numb":"2","Total":"6
        "},{"Year":"2016-11-20","Numb":"1","Total":"6
        "},{"Year":"2016-11-22","Numb":"1","Total":"6
        "},{"Year":"2016-12-05","Numb":"1","Total":"6
        "},{"Year":"2016-12-06","Numb":"1","Total":"6
        "},{"Year":"2016-12-07","Numb":"1","Total":"6
        "},{"Year":"2016-10-27","Numb":"1","Total":"7
        "},{"Year":"2016-10-31","Numb":"1","Total":"7
        "},{"Year":"2016-11-02","Numb":"1","Total":"7
        "},{"Year":"2016-11-05","Numb":"1","Total":"7
        "},{"Year":"2016-11-07","Numb":"1","Total":"7
        "},{"Year":"2016-11-08","Numb":"1","Total":"7
        "},{"Year":"2016-11-09","Numb":"1","Total":"7
        "},{"Year":"2016-11-10","Numb":"2","Total":"7
        "},{"Year":"2016-11-16","Numb":"2","Total":"7
        "},{"Year":"2016-11-20","Numb":"1","Total":"7
        "},{"Year":"2016-11-22","Numb":"1","Total":"7
        "},{"Year":"2016-12-05","Numb":"1","Total":"7
        "},{"Year":"2016-12-06","Numb":"1","Total":"7
        "},{"Year":"2016-12-07","Numb":"1","Total":"7
        "},{"Year":"2016-10-27","Numb":"1","Total":"9
        "},{"Year":"2016-10-31","Numb":"1","Total":"9
        "},{"Year":"2016-11-02","Numb":"1","Total":"9
        "},{"Year":"2016-11-05","Numb":"1","Total":"9
        "},{"Year":"2016-11-07","Numb":"1","Total":"9
        "},{"Year":"2016-11-08","Numb":"1","Total":"9
        "},{"Year":"2016-11-09","Numb":"1","Total":"9
        "},{"Year":"2016-11-10","Numb":"2","Total":"9
        "},{"Year":"2016-11-16","Numb":"2","Total":"9
        "},{"Year":"2016-11-20","Numb":"1","Total":"9
        "},{"Year":"2016-11-22","Numb":"1","Total":"9
        "},{"Year":"2016-12-05","Numb":"1","Total":"9
        "},{"Year":"2016-12-06","Numb":"1","Total":"9
        "},{"Year":"2016-12-07","Numb":"1","Total":"9
        "},{"Year":"2016-10-27","Numb":"1","Total":"11
        "},{"Year":"2016-10-31","Numb":"1","Total":"11
        "},{"Year":"2016-11-02","Numb":"1","Total":"11
        "},{"Year":"2016-11-05","Numb":"1","Total":"11
        "},{"Year":"2016-11-07","Numb":"1","Total":"11
        "},{"Year":"2016-11-08","Numb":"1","Total":"11
        "},{"Year":"2016-11-09","Numb":"1","Total":"11
        "},{"Year":"2016-11-10","Numb":"2","Total":"11
        "},{"Year":"2016-11-16","Numb":"2","Total":"11
        "},{"Year":"2016-11-20","Numb":"1","Total":"11
        "},{"Year":"2016-11-22","Numb":"1","Total":"11
        "},{"Year":"2016-12-05","Numb":"1","Total":"11
        "},{"Year":"2016-12-06","Numb":"1","Total":"11
        "},{"Year":"2016-12-07","Numb":"1","Total":"11
        "},{"Year":"2016-10-27","Numb":"1","Total":"12
        "},{"Year":"2016-10-31","Numb":"1","Total":"12
        "},{"Year":"2016-11-02","Numb":"1","Total":"12
        "},{"Year":"2016-11-05","Numb":"1","Total":"12
        "},{"Year":"2016-11-07","Numb":"1","Total":"12
        "},{"Year":"2016-11-08","Numb":"1","Total":"12
        "},{"Year":"2016-11-09","Numb":"1","Total":"12
        "},{"Year":"2016-11-10","Numb":"2","Total":"12
        "},{"Year":"2016-11-16","Numb":"2","Total":"12
        "},{"Year":"2016-11-20","Numb":"1","Total":"12
        "},{"Year":"2016-11-22","Numb":"1","Total":"12
        "},{"Year":"2016-12-05","Numb":"1","Total":"12
        "},{"Year":"2016-12-06","Numb":"1","Total":"12
        "},{"Year":"2016-12-07","Numb":"1","Total":"12
        "},{"Year":"2016-10-27","Numb":"1","Total":"13
        "},{"Year":"2016-10-31","Numb":"1","Total":"13
        "},{"Year":"2016-11-02","Numb":"1","Total":"13
        "},{"Year":"2016-11-05","Numb":"1","Total":"13
        "},{"Year":"2016-11-07","Numb":"1","Total":"13
        "},{"Year":"2016-11-08","Numb":"1","Total":"13
        "},{"Year":"2016-11-09","Numb":"1","Total":"13
        "},{"Year":"2016-11-10","Numb":"2","Total":"13
        "},{"Year":"2016-11-16","Numb":"2","Total":"13
        "},{"Year":"2016-11-20","Numb":"1","Total":"13
        "},{"Year":"2016-11-22","Numb":"1","Total":"13
        "},{"Year":"2016-12-05","Numb":"1","Total":"13
        "},{"Year":"2016-12-06","Numb":"1","Total":"13
        "},{"Year":"2016-12-07","Numb":"1","Total":"13
        "},{"Year":"2016-10-27","Numb":"1","Total":"14
        "},{"Year":"2016-10-31","Numb":"1","Total":"14
        "},{"Year":"2016-11-02","Numb":"1","Total":"14
        "},{"Year":"2016-11-05","Numb":"1","Total":"14
        "},{"Year":"2016-11-07","Numb":"1","Total":"14
        "},{"Year":"2016-11-08","Numb":"1","Total":"14
        "},{"Year":"2016-11-09","Numb":"1","Total":"14
        "},{"Year":"2016-11-10","Numb":"2","Total":"14
        "},{"Year":"2016-11-16","Numb":"2","Total":"14
        "},{"Year":"2016-11-20","Numb":"1","Total":"14
        "},{"Year":"2016-11-22","Numb":"1","Total":"14
        "},{"Year":"2016-12-05","Numb":"1","Total":"14
        "},{"Year":"2016-12-06","Numb":"1","Total":"14
        "},{"Year":"2016-12-07","Numb":"1","Total":"14
        "},{"Year":"2016-10-27","Numb":"1","Total":"15
        "},{"Year":"2016-10-31","Numb":"1","Total":"15
        "},{"Year":"2016-11-02","Numb":"1","Total":"15
        "},{"Year":"2016-11-05","Numb":"1","Total":"15
        "},{"Year":"2016-11-07","Numb":"1","Total":"15
        "},{"Year":"2016-11-08","Numb":"1","Total":"15
        "},{"Year":"2016-11-09","Numb":"1","Total":"15
        "},{"Year":"2016-11-10","Numb":"2","Total":"15
        "},{"Year":"2016-11-16","Numb":"2","Total":"15
        "},{"Year":"2016-11-20","Numb":"1","Total":"15
        "},{"Year":"2016-11-22","Numb":"1","Total":"15
        "},{"Year":"2016-12-05","Numb":"1","Total":"15
        "},{"Year":"2016-12-06","Numb":"1","Total":"15
        "},{"Year":"2016-12-07","Numb":"1","Total":"15
        "},{"Year":"2016-10-27","Numb":"1","Total":"16
        "},{"Year":"2016-10-31","Numb":"1","Total":"16
        "},{"Year":"2016-11-02","Numb":"1","Total":"16
        "},{"Year":"2016-11-05","Numb":"1","Total":"16
        "},{"Year":"2016-11-07","Numb":"1","Total":"16
        "},{"Year":"2016-11-08","Numb":"1","Total":"16
        "},{"Year":"2016-11-09","Numb":"1","Total":"16
        "},{"Year":"2016-11-10","Numb":"2","Total":"16
        "},{"Year":"2016-11-16","Numb":"2","Total":"16
        "},{"Year":"2016-11-20","Numb":"1","Total":"16
        "},{"Year":"2016-11-22","Numb":"1","Total":"16
        "},{"Year":"2016-12-05","Numb":"1","Total":"16
        "},{"Year":"2016-12-06","Numb":"1","Total":"16
        "},{"Year":"2016-12-07","Numb":"1","Total":"16
        "}]

非常感谢任何帮助。这个问题超出了我的范围。

1 个答案:

答案 0 :(得分:0)

如果你谷歌正确的问题帮助......只需将二合一...... ;-(

$myArray = array();

if ($result = $mysqli->query("SELECT DATE_FORMAT(DateAdded, '%Y-%m-%d') AS Year, COUNT(*) AS Numb, @running_sum:=@running_sum + d.count AS Total FROM (SELECT DateAdded, COUNT(*) AS 'count' FROM details WHERE MemberStatus = 'Active' AND PaymentStatus NOT REGEXP 'Not' GROUP BY DateAdded ORDER BY DateAdded) d JOIN (SELECT @running_sum := 0 AS dummy) dummy GROUP BY DATE_FORMAT(DateAdded,'%Y-%m-%d');"))   {
    while($row = $result->fetch_array(MYSQL_ASSOC)) {
           $myArray[] = $row;
    }
echo json_encode($myArray);
}