功能描述:利用mysql+php做一个php接口,根据时间dtime把状态=‘pc’和状态=‘phone’进行汇总,全部数据进行汇总,输出结果如下格式:
[ {"dtime": "2024-12-28","pc_count": "0","phone_count": "1", "total_count":"1"}, {"dtime": "2024-12-31","pc_count": "1","phone_count": "0", "total_count":"1"}, {"dtime": "2025-01-02","pc_count": "1","phone_count": "0", "total_count":"1"} ]
具体实现语法如下:
$sql = 'SELECT DATE(表名.dtime) AS dtime, SUM(CASE WHEN 表名.msg = "pc" THEN 1 ELSE 0 END) AS pc_count, SUM(CASE WHEN 表名.msg = "phone" THEN 1 ELSE 0 END) AS phone_count, SUM(CASE WHEN 表名.msg IN ("pc", "phone") THEN 1 ELSE 0 END) AS total_count FROM 表名 WHERE 表名.mid = "'.$id.'" AND 表名.dtime >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY DATE(表名.dtime)';