TinkPHP5中汇总一些常见的查询写法


常用到的联合查询,范围查询,条件查询,时间查询,分页查询。每次用到每次百度,虽然简单,但是麻烦。记录汇总一下:

        //多条件分页查询保留参数
        $pagenum = request()->param('limit',10);
        //模糊查询
        $map['a|b|c'] = array('like','%'.$keywords.'%');//a或者b或者c均模糊匹配
        //用|分割表示OR查询,用&分割表示AND查询
        //不为空查询
        $map['field'] = ['neq','not null']; //不为空(推荐用whereNotNull)
        $map['field'] = null;       //null
        //id范围查询
        $map['id'] = array('neq',100);//不等于100
        $map['id'] = array('gt',100);//大于100
        $map['id'] = array('egt',100);//大于等于100
        $map['id'] = array('between','1,8');
        $map['id']=array('in','1,2,3');
        //时间查询
        $map['create_time']=['> time','2016-1-1'];
        $map['create_time']=['<= time','2016-1-1'];
        $map['create_time']=['between time',['2015-1-1','2016-1-1']];
        $declares_mod = new model\Dclares();
        $declares = $declares_mod
            ->alias("d")
            ->join('org o', 'd.orgid = o.orgid')
            ->join('test_paper_user u',"u.paper_id=p.id and u.deleted=0 and u.is_finish=0 and u.user_id='".$Session['user_id']."'")
            ->field('d.image,d.id,d.addtime,d.create_time,d.status,o.org_name,d.url,d.accounttitle,d.accountid,d.fans')
            ->where($map)
            ->whereTime('create_time', 'today')//时间介于今天
            ->whereNull('email')
            ->whereNotNull('name')
            ->order('d.status desc,d.id desc')
            ->paginate($pagenum,false,['query'=>request()->param()]);
        $count = $declares->total();
        $declaresData = $declares->all();
        for($i=0; $i<count($declaresData); $i++){
            $declaresData[$i]['create_time_name'] = date("Y-m-d", $declaresData[$i]['create_time']);
        }


最新回复 (0)
发新帖