php 年月日 分组分页

//年月日 //分组 分页
        $type = $this->request->type;
        $dateType = "%Y-%m";//月
        $dateType1 = "CONCAT(tmp.date,'-01 00:00:00')";
        $dateType2 = "CONCAT(LAST_DAY(CONCAT(tmp.date, '-15')),' 23:59:59')";
        if ($type == 'day') {//日
            $dateType = "%Y-%m-%d";
            $dateType1 = "CONCAT(tmp.date,' 00:00:00')";
            $dateType2 = "CONCAT(tmp.date,' 23:59:59')";
        }elseif ($type == 'year') {//年
            $dateType = "%Y";
            $dateType1 = "CONCAT(tmp.date,'-01-01 00:00:00')";
            $dateType2 = "CONCAT(tmp.date,'-12-31 23:59:59')";
        }

        $sql = "SELECT (
                    SELECT CONCAT(sum(go.pv),'-',sum(go.buy_money),'-',count(go.id),'-',sum(go.number)) FROM orders go WHERE go.uid IN (
                        SELECT id FROM user WHERE recommend_path LIKE concat(u.recommend_path,',',u.id,',%') OR recommend_uid = u.id
                    ) AND go.pay_time >= {$dateType1} AND go.pay_time <= {$dateType2} {$whereGroupOrderSql}
                ) as group_total, (
                        SELECT CONCAT(sum(mo.pv),'-',sum(mo.buy_money),'-',count(mo.id),'-',sum(mo.number)) 
                        FROM orders mo
                        WHERE mo.uid = u.id {$whereMyOrderSql} AND mo.pay_time >= {$dateType1} AND mo.pay_time <= {$dateType2}
                ) as my_total, u.username, tmp.date
                FROM `user` u, (SELECT date_format(pay_time,'{$dateType}') as date FROM orders GROUP BY date) tmp
                WHERE 1 {$whereSql}
                GROUP BY tmp.date, u.id ORDER BY tmp.date desc, u.id 
                LIMIT {$start}, {$limit}";

        $records = Db::query($sql);

你可能感兴趣的:(PHP工作后遇到的问题,ThinkPHP,php,开发语言)