如何根据自己的日期对记录进行计数。我只是在我的mysql查询中使用BETWEEN传递两个日期,例如2017-05-01 and 2017-05-10
表:
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
我想要这样的结果
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
我的尝试
$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));
它将提供错误的输出以及循环不可阻挡。如何在mysql中的日期之间使用日期计数。请指导我。
答案 0 :(得分:2)
没有必要使用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;