一个关于按月分组查询的例子

2022-03-02 11:32374
public function scoreList()
    {
        $list = [];
        $items= [];
        $user_id = $this->getUserId();
        $smodel = new UserScoreLogModel();
        $datelist=$smodel->field("FROM_UNIXTIME(create_time, '%Y-%m') as mDate")->group('mDate')->order('mDate desc')->where('user_id',$user_id)->select()->toArray();
        foreach($datelist as $key=>$value){
            $items= $smodel->field("id,create_time,action as note,score")->where('user_id',$user_id)->whereMonth('create_time', $value['mDate'])->select();
            array_push($list,['title'=>$value['mDate'],'items'=>$items]);
        }

        $this->success('获取成功!', ['list' => $list]);
    }
{
    "code": 1,
    "msg": "获取成功!",
    "data": {
        "list": [
            {
                "title": "2022年1月",
                "items": [
                    {
                        "note": "天天签到积分",
                        "score": 5,
                        "create_time": 1641540944
                    },
                    {
                        "note": "天天签到积分",
                        "score": 5,
                        "create_time": 1641454544
                    },
                    {
                        "note": "天天签到积分",
                        "score": 5,
                        "create_time": 1641368144
                    }
                ]
            },
            {
                "title": "2021年12月",
                "items": [
                    {
                        "note": "天天签到积分",
                        "score": 5,
                        "create_time": 1641540944
                    },
                    {
                        "note": "天天签到积分",
                        "score": 5,
                        "create_time": 1641454544
                    },
                    {
                        "note": "天天签到积分",
                        "score": 5,
                        "create_time": 1641368144
                    }
                ]
            }
        ]
    }
}


上一篇: ThinkPHP中查询获取随机数据,随机文章列表

下一篇: PHP换行符转br