where条件->where("type",null)//判断为空->where("type",'notnull')//判断不为空完整DB查询语句Db::table('user')->where("type",'notnull')->select();
{switchname="变量"}{casevalue="值"}值1{/case}{casevalue="值"}值2{/case}{casevalue="值"}值3{/case}{default/}默认值{/switch}
控制器调用publicfunctionGetRanStr(){if(request()->isPost()){ //生成6位数随机数returnGetRandStr(6); } }公共方法/***生成随机数*@param$len*@returnstring*/functionGetRandStr($len){$chars=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");$charsLen=count($chars)-1;shuffle($chars);$output="";for($i=0;$i<$len;$i++){$output.=$chars[mt_rand(0,$charsLen)];}return$output;}
常用的tp5多条件查询拼接1,Where条件表达式格式为:$map['字段名']=array('表达式','操作条件');2,以查询user表为例$User=model("User");//实例化User对象方法1: $User->where(['type'=>1,'status'=>['in',[1,2]],'regtime'=>['>=',strtotime('-1day')]])->select();方法2:$User->where(['type'=>1,'status'=>['in',[1,2]],'regtime'=>['>=',strtotime('-1day')]])->select();方法3:$User->where(['status'=>1])->whereor(['status'=>2])->select();方法4:$User->where(['name'=>'张三','status'=>1])->where(function($query){$query->where(['age'=>20])->whereor(['sex'=>1]);})->find();方法5:$User->where(['uid'=>['gt',1],'_string'=>'(namelike"%张%")OR(titlelike"%测试%")'])->select();
快捷查询where('id&age','>',0);where('id|age','>',0);闭包查询$result=Db::name('data')->select(function($query){$query->where('name','like','%think%')->where('id','in','1,2,3')->limit(10);});$result=Db::name('data')->select(function($query){$query->where('name','like','%think%')->where('id','between',[1,3])->limit(10);});获取列数据,并且以id为索引$list=Db::name('data')->where('status',1)->column('name','id');聚合查询Db::name('data')->where('id','>',1)->count();Db::name('data')->where('id','>',1)->max('age');字符串查询$result=Db::table('user')->where('id>:idandnameisnotnull',['id'=>10])->select();日期时间查询查询大于某日的数据$result=Db::table('user')->whereTime('create_time','>','2017-01-01')->select();查询本周的数据$result=Db::table('user')->whereTime('create_time','week')->select();查询最近两天添加的数据$result=Db::table('user')->whereTime('create_time','-2days')->select();查询一个时间范围的数据$result=Db::table('user')->whereTime('create_time','between',['2017-1-1','2017-1-10'])->select();查询上周的数据$result=Db::table('user')->whereTime('create_time','lastweek')->select();
在项目中 可能会遇到 跨月份进行查询比如在当输入201809 会获取当月的开始时间$start_month 和结束时间$end_month会查询2018年9月份的数据 但是当其中的一个数据是在201809到201810,数据库的字段是 start_time end_time这时候Db::name("表名")->where('start_time','<=time',$end_month)->where('end_time','>time',$start_month)->select();时间比较使用where方法where方法支持时间比较,例如://大于某个时间where('create_time','>time','2016-1-1');//小于某个时间where('create_time','<=time','2016-1-1');//时间区间查询where('create_time','betweentime',['2015-1-1','2016-1-1']);使用whereTime方法whereTime方法提供了日期和时间字段的快捷查询,示例如下://大于某个时间Db::table('think_user')->whereTime('birthday','>=','1970-10-1')->select();//小于某个时间Db::table('think_user')->whereTime('birthday','<','2000-10-1')->select();//时间区间查询Db::table('think_user')->whereTime('birthday','between',['1970-10-1','2000-10-1'])->select();//不在某个时间区间Db::table('think_user')->whereTime('birthday','notbetween',['1970-10-1','2000-10-1'])->select();时间表达式还提供了更方便的时间表达式查询,例如://获取今天的博客Db::table('think_blog')->whereTime('create_time','today')->select();//获取昨天的博客Db::table('think_blog')->whereTime('create_time','yesterday')->select();//获取本周的博客Db::table('think_blog')->whereTime('create_time','week')->select();//获取上周的博客Db::table('think_blog')->whereTime('create_time','lastweek')->select();//获取本月的博客Db::table('think_blog')->whereTime('create_time','month')->select();//获取上月的博客Db::table('think_blog')->whereTime('create_time','lastmonth')->select();//获取今年的博客Db::table('think_blog')->whereTime('create_time','year')->select();//获取去年的博客Db::table('think_blog')->whereTime('create_time','lastyear')->select();如果查询当天、本周、本月和今年的时间,还可以简化为://获取今天的博客Db::table('think_blog')->whereTime('create_time','d')->select();//获取本周的博客Db::table('think_blog')->whereTime('create_time','w')->select();//获取本月的博客Db::table('think_blog')->whereTime('create_time','m')->select();//获取今年的博客Db::table('think_blog')->whereTime('create_time','y')->select();V5.0.5+版本开始,还可以使用下面的方式进行时间查询//查询两个小时内的博客Db::table('think_blog')->whereTime('create_time','-2hours')->select();
//如不加第二个参数,默认值为1//score字段加1Db::table('think_user')->where('id',1)->setInc('score');//score字段加5Db::table('think_user')->where('id',1)->setInc('score',5);//score字段减1Db::table('think_user')->where('id',1)->setDec('score');//score字段减5Db::table('think_user')->where('id',1)->setDec('score',5)
$params=input('param.');//获取所有,不分get、post、put。返回数组$gets=input('get.');//获取所有get请求来的参数。返回数组$posts=input('post.');//返回数组$id=input('get.id');//获取get来的id的值,返回字符串$password=input('post.password');$id=Request::instance()->param('id');publicfunctionhello(Request$request){echo'请求参数';dump(input());echo'name:'.$request->param('name');echo'类型:'.$request->type().'<br/>';}
获取当前时间:$now=time();时间戳转换正常时间格式: date('Y-m-dH:i:s',time());日期转换为时间戳:$date="2013-10-0112:23:14";strtotime($date);model模型:自动写入时间戳字段(true、false):protected$autoWriteTimestamp=true;时间字段取出后的默认时间格式:protected$dateFormat='Y-m-d';
1.当统计一个有重复的字段可以用这个方法(统计数量时去重id)$count=$model->where($map)->count('distinct(id)');2.利用distinct方法去重$data=$test_data->Distinct(true)->field('descriprion')->order('descriptiondesc')->select();3.利用group方法去重$data=$test_data->group('description')->order('descriptiondesc')->select();对于两种去重方式: 利用distinct去重、简单易用,但只能对于单一字段去重,并且最终的结果也仅为去重的字段,实际应用价值不是特别大。利用group去重,最终的显示结果为所有字段,且对单一字段进行了去重操作,效果不错,但最终显示结果除去去重字段外,按照第一个字段进行排序,可能还需要处理。