简体   繁体   English

如何使用mysql每天计数记录

[英]How to Count a record per day by day using mysql

How to Count the records as per the date it self.I just pass the two dates using BETWEEN in my mysql query eg for 2017-05-01 and 2017-05-10 如何按其自身的日期计数记录。我只是在MySQL查询中使用BETWEEN传递两个日期,例如2017-05-01 and 2017-05-10

Table: 表:

 tid    vid     dates       type
-------------------------------------
 1  TN01VD2365  2017-05-01  Cash
 2  TN01VD1254  2017-05-02  Cash
 3  TN03JG2589  2017-05-01  Credit
 4  TN12KL5874  2017-05-01  Cash
 5  TN14DS4569  2017-05-05  Compliment
 6  TN45KJ6987  2017-05-06  Credit
 7  TN45AS6542  2017-05-06  Cash
 8  TN78DF6589  2017-05-10  Complimant

i want the result like this 我想要这样的结果

dates        typeCash       typeCredit      typeCompliment
---------------------------------------------------------------
2017-05-01       2           1             0
2017-05-02       1           0             0
2017-05-05       0           0             1
2017-05-06       1           1             0
2017-05-10       0           0             1

My try 我的尝试

  $json="";

    $created_date1="5-1-2017";
    $created_date2="5-10-2017";


    if(isset($created_date1))
    {
        $x=explode("-",$created_date1);
        $created_date1=$x[1]."-".$x[0]."-".$x[2];
        $created_date1 = strtotime($created_date1);
        $created_date1 = date('Y-m-d',$created_date1);  
    }
if(isset($created_date2))
{
    $y=explode("-",$created_date2);
    $created_date2=$y[1]."-".$y[0]."-".$y[2];
    $created_date2 = strtotime("$created_date2");
    $created_date2 = date('Y-m-d',$created_date2);
}
    $date2=$created_date2;
    $i=0;
    while($created_date1<=$date2)
    {
        $created_date2=$created_date1;


                $mycount = "select 
                                    count(tid) as mycount 

                                    from 

                                    third_table 

                                    where 

                                    dates BETWEEN '".$created_date1."' AND '".$created_date2."'";


                $execte=mysql_query($mycount);
                $mynum=mysql_fetch_array($execte);
                $mynum_count=$mynum['mycount'];

            if($mynum_count>0)
            {
                    $trip_per_day="select 
                                    a.eid,a.name,
                                    count(b.tid) as trips_per_day,
                                    COUNT(IF(b.type_of_trip='Credit',1,null)) as credit_trips,
                                    COUNT(IF(b.type_of_trip='Compliment',1,null)) as compliment_trips 
                                    b.dates 
                                    from third_table b 
                                    LEFT JOIN add_employees a ON b.emp_id=a.eid 
                                    where b.dates between '$created_date1' and '$created_date2' 
                                    group by b.tid";

                    $run_qry=mysql_query($trip_per_day);
                    $row = mysql_fetch_array($run_qry);
                    $name = $row['name'];
                    $trip_per_day = $row['trips_per_day'];
                    $cash_trips = $row['cash_trips'];
                    $credit_trips = $row['credit_trips'];
                    $compliment_trips = $row['compliment_trips'];

                    $arr[$i]["name"]=$name;
                    $arr[$i]["date"]=$created_date1;//particular data
                    $arr[$i]["trips_per_day"] = $trip_per_day;//for trip_per_day
                    $arr[$i]["cash_trips"] = $cash_trips;//for cash_trips
                    $arr[$i]["credit_trips"] = $credit_trips;//for credit_trips
                    $arr[$i]["compliment_trips"] = $compliment_trips;//for compliment_trips
            }
            $inc_qry="select '".$created_date1."' + INTERVAL 1 DAY";
            $query=mysql_query($inc_qry);
            while($val=mysql_fetch_array($query))
            {
                $created_date1=$val[0];
            }
            $i++;
    }
    $json['all_counts_reports']=$arr;
    $json=json_encode($json);

    $array = json_decode($json,true);
    $array['all_counts_reports']=array_values($array['all_counts_reports']);
    //var_export(json_encode($array));
    echo $result = str_replace('', '', json_encode($array));

it will give the wrong output as well as looping unstoppable.How to get Day by day count using between dates in mysql. 它将给出错误的输出以及无法停止的循环。如何在mysql中使用日期之间的日计数。 please guide me . 请指导我。

There is no need to use php code to do the job you want, as you can get the required output using conditional aggregation in a single SQL query: 无需使用php代码即可完成所需的工作,因为您可以在单个SQL查询中使用条件聚合来获得所需的输出:

SELECT dates,
       SUM(type = 'Cash') AS typeCash,
       SUM(type = 'Credit') AS typeCredit,
       SUM(type = 'Compliment') AS typeCompliment
FROM mytable
WHERE dates BETWEEN '2017-05-01' AND '2017-05-10'
GROUP BY dates;

Demo here 在这里演示

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

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