在echarts中需要实现最近24小时和上24小时,即当天与昨天24小时内时间段的数据统计,mysql语句如下:
//最近24小时记录;
$sql = "select RIGHT(DATE_FORMAT(paid_at,'%Y-%m-%d %H'),2) as dataKey,ifnull(COUNT(id),0) AS totalTrans,ifnull(SUM(should_paid),0) AS totalPays from " . $db->table('orders') . " WHERE DATE_FORMAT(paid_at,'%Y-%m-%d') = curdate()";
$sql .= " GROUP BY DATE_FORMAT(paid_at,'%Y-%m-%d %H')";
$row = $db->queryall($sql);
$res["data"] = $row;
//上24小时记录;
$sql_p = "select RIGHT(DATE_FORMAT(paid_at,'%Y-%m-%d %H'),2) as dataKey,ifnull(COUNT(id),0) AS totalTrans,ifnull(SUM(should_paid),0) AS totalPays from " . $db->table('orders') . " WHERE TO_DAYS(curdate()) - TO_DAYS(date_format(paid_at,'%Y-%m-%d')) = 1";
$sql_p .= " GROUP BY DATE_FORMAT(paid_at,'%Y-%m-%d %H')";
$row_p = $db->queryall($sql_p);
$res["pdata"] = $row_p;
die(json_encode_lockdata($res));
说明:
RIGHT(date(),2) //截取时间段后两位,即00,01,02...
GROUP BY DATE_FORMAT(paid_at,'%Y-%m-%d %H')
TO_DAYS(curdate()) - TO_DAYS(date_format(paid_at,'%Y-%m-%d')) = 1
最终生成的数据格式:
{"err":"","res":"",
"data":[{"dataKey":"00","totalTrans":"4","totalPays":"301.70"},{"dataKey":"01","totalTrans":"1","totalPays":"39.90"},{"dataKey":"03","totalTrans":"2","totalPays":"35.80"},{"dataKey":"05","totalTrans":"3","totalPays":"80.80"},{"dataKey":"06","totalTrans":"9","totalPays":"423.90"},{"dataKey":"07","totalTrans":"38","totalPays":"1925.69"},{"dataKey":"08","totalTrans":"91","totalPays":"4324.88"},{"dataKey":"09","totalTrans":"119","totalPays":"6423.51"},{"dataKey":"10","totalTrans":"128","totalPays":"9273.93"},{"dataKey":"11","totalTrans":"123","totalPays":"6563.52"},{"dataKey":"12","totalTrans":"81","totalPays":"4116.21"},{"dataKey":"13","totalTrans":"58","totalPays":"2704.50"},{"dataKey":"14","totalTrans":"77","totalPays":"6561.20"},{"dataKey":"15","totalTrans":"40","totalPays":"2078.60"},{"dataKey":"16","totalTrans":"75","totalPays":"4270.22"},{"dataKey":"17","totalTrans":"193","totalPays":"7928.64"},{"dataKey":"18","totalTrans":"81","totalPays":"3809.69"},{"dataKey":"19","totalTrans":"30","totalPays":"1409.47"}],
"pdata":[{"dataKey":"00","totalTrans":"5","totalPays":"173.20"},{"dataKey":"01","totalTrans":"1","totalPays":"25.90"},{"dataKey":"05","totalTrans":"2","totalPays":"59.80"},{"dataKey":"06","totalTrans":"9","totalPays":"279.80"},{"dataKey":"07","totalTrans":"9","totalPays":"304.00"},{"dataKey":"08","totalTrans":"28","totalPays":"1213.80"},{"dataKey":"09","totalTrans":"62","totalPays":"3588.53"},{"dataKey":"10","totalTrans":"73","totalPays":"5576.20"},{"dataKey":"11","totalTrans":"77","totalPays":"4690.90"},{"dataKey":"12","totalTrans":"60","totalPays":"3712.68"},{"dataKey":"13","totalTrans":"51","totalPays":"2377.80"},{"dataKey":"14","totalTrans":"60","totalPays":"2746.80"},{"dataKey":"15","totalTrans":"63","totalPays":"3833.20"},{"dataKey":"16","totalTrans":"61","totalPays":"3271.10"},{"dataKey":"17","totalTrans":"49","totalPays":"3171.60"},{"dataKey":"18","totalTrans":"59","totalPays":"4038.30"},{"dataKey":"19","totalTrans":"75","totalPays":"4575.40"},{"dataKey":"20","totalTrans":"116","totalPays":"4818.44"},{"dataKey":"21","totalTrans":"136","totalPays":"7200.34"},{"dataKey":"22","totalTrans":"58","totalPays":"2934.00"},{"dataKey":"23","totalTrans":"25","totalPays":"1904.00"}]}
Done!