作者:下个路口见的 | 来源:互联网 | 2014-06-12 18:37
ThinkPHP中的查询语言:普遍查询1)字符串形式$list$user->where('usernamebbbb')->select();2)数组形式$data['username']...
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[&#39;id&#39;] = array(array(&#39;gt&#39;,3),array(&#39;lt&#39;,10), &#39;or&#39;) ;
- $map[&#39;name&#39;] = array(array(&#39;like&#39;,&#39;%a%&#39;), array(&#39;like&#39;,&#39;%b%&#39;), array(&#39;like&#39;,&#39;%c%&#39;), &#39;ThinkPHP&#39;,&#39;or&#39;);
对应的查询条件是:(`name` LIKE &#39;%a%&#39;) OR (`name` LIKE &#39;%b%&#39;) OR (`name` LIKE &#39;%c%&#39;) OR (`name` = &#39;ThinkPHP&#39;);
组合查询:
1)字符串模式查询(_string)
- $user=M(&#39;User&#39;);
- $data[id]=array(&#39;neq&#39;,1);
- $data[&#39;username&#39;]=&#39;aaaaa&#39;;
- $data[&#39;_string&#39;]=&#39;userpass=123 and createtime=2012&#39;;
- $list=$user->where($data)->select();
2)请求字符串查询方式
- $data[&#39;id&#39;]=array(&#39;gt&#39;,100);
- $data[&#39;_query&#39;]=&#39;userpass=1&username=aa&_logic=or&#39;;
- $list=$user->where($data)->select();
3)复合查询
- $wh[&#39;username&#39;]=array(&#39;like&#39;,&#39;%thinkphp%&#39;);
- $wh[&#39;userpass&#39;]=array(&#39;like&#39;,&#39;3%&#39;);
- $wh[&#39;_logic&#39;]=&#39;or&#39;;
- $data[&#39;_complex&#39;]=$wh;
- $data[&#39;id&#39;]=array(&#39;gt&#39;,100);
- $list=$user->where($data)->select();
对应于:(id>100)AND( (namelike&#39;%thinkphp%&#39;)OR(titlelike&#39;%thinkphp%&#39;) )
统计查询
- count():$num=$user->count();$num=$user->count(&#39;id&#39;);
- max():
- min():
- avg():
- sum():
定位查询
要求当前模型必须继承高级模型类才能使用
- $User->where(&#39;score>0&#39;)->order(&#39;score desc&#39;)->getN(2);
- $User-> where(&#39;score>80&#39;)->order(&#39;score desc&#39;)->getN(-2);
- $User->where(&#39;score>80&#39;)->order(&#39;score desc&#39;)->first();
- $User->where(&#39;score>80&#39;)->order(&#39;score desc&#39;)->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=&#39;thinkPHP&#39; where status=1");
动态查询
- $user = $User->getByName(&#39;liu21st&#39;);
- $user = $User->getFieldByName(&#39;liu21st&#39;,&#39;id&#39;);
- $user-> where(&#39;score>80&#39;)->order(&#39;score desc&#39;)->top5();