ThinkPHP中的查詢語言:
普遍查詢
1)字符串形式
$list=$user->where('username=bbbb')->select();
2)數組形式
- $data['username']='bbbbb';
- $list=$user->where($data)->select();
3)對象形式
- $user=M('user');
- $a=new stdClass();
- $a->username='bbbbb';
- $list=$user->where($a)->select();
查詢表達式
EQ (=)
NEQ(!=)
GT (>) LT(<) [NOT]BETWEEN(對應sql中的between) IN
EGT(>=)ELT(<=)LIKE (對應sql中的like)
EXP(使用標準sql語句實現較復雜的情況)
區間查詢
- $map['id'] = array(array('gt',3),array('lt',10), 'or') ;
- $map['name'] = array(array('like','%a%'), array('like','%b%'), array('like','%c%'), 'ThinkPHP','or');
對應的查詢條件是:(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'ThinkPHP');
組合查詢:
1)字符串模式查詢(_string)
- $user=M('User');
- $data[id]=array('neq',1);
- $data['username']='aaaaa';
- $data['_string']='userpass=123 and createtime=2012';
- $list=$user->where($data)->select();
2)請求字符串查詢方式
- $data['id']=array('gt',100);
- $data['_query']='userpass=1&username=aa&_logic=or';
- $list=$user->where($data)->select();
3)復合查詢
- $wh['username']=array('like','%thinkphp%');
- $wh['userpass']=array('like','3%');
- $wh['_logic']='or';
- $data['_complex']=$wh;
- $data['id']=array('gt',100);
- $list=$user->where($data)->select();
對應于:(id>100)AND( (namelike'%thinkphp%')OR(titlelike'%thinkphp%') )
統計查詢
- count():$num=$user->count();$num=$user->count('id');
- max():
- min():
- avg():
- sum():
定位查詢
要求當前模型必須繼承高級模型類才能使用
- $User->where('score>0')->order('score desc')->getN(2);
- $User-> where('score>80')->order('score desc')->getN(-2);
- $User->where('score>80')->order('score desc')->first();
- $User->where('score>80')->order('score desc')->last();
SQL查詢
- $model=new Model();
- $list=$model->query("select * from think_user where id>1 and id<10");
- $model=new Model();
- $Model->execute("update think_user set name='thinkPHP' where status=1");
動態查詢
- $user = $User->getByName('liu21st');
- $user = $User->getFieldByName('liu21st','id');
- $user-> where('score>80')->order('score desc')->top5();
新聞熱點
疑難解答
圖片精選