一、普通查询
在查询带入where条件等,最少有三种形式
EQ = NEQ != GT > EGT >= LT <div> ELT <= LIKE like [NOT]BETWEEN [not]between [not]in
推荐使用数组形式查询
1、字符串形式:
&#39;id>5 and id<9&#39;
2、数组形式
$user=M(&#39;user&#39;); $data[&#39;username&#39;]=&#39;liwenkai&#39;; $list=$user->where(array(&#39;username&#39;=>&#39;liwenkai&#39;))->select(); $list=$user->where($data)->select();
3、对象形式
$user=M(&#39;user&#39;); $a=new stdClass(); $a->username=&#39;liwenkai&#39;; $list=$user->where($a)->select();
4、查询表达式
EQ 等于 NEQ 不等于 GT 大于 EGT 大于等于 LT 小于 ELT 小于等于 LIKE 等价与sql中的like [NOT] BETWEEN 查询区间 [NOT] IN 查询集合 EXP 指使用标准SQL语句,实现更加复杂的情况 $data[&#39;字段名&#39;]=array(&#39;是表达式&#39;,&#39;查询条件&#39;); $data[&#39;liwenkai&#39;]=&#39;liwenkai&#39;;实际上是指$data[&#39;liwenkai&#39;]=array(&#39;eq&#39;,&#39;liwenkai&#39;); $data[&#39;username&#39;]=array(&#39;like&#39;,&#39;peng%&#39;); $list=$user->where($data)->select();
二、区间查询
$user=M(&#39;user&#39;); $data[&#39;id&#39;]=array(array(&#39;gt&#39;,20),array(&#39;lt&#39;,23),&#39;and&#39;); $list=$user->where($data)->select(); dump($list); $data[&#39;username&#39;]=array(array(&#39;like&#39;,&#39;p%&#39;),array(&#39;like&#39;,&#39;h%&#39;),&#39;or&#39;);
三、组合查询
$user=M(&#39;user&#39;); $data[&#39;username&#39;]=&#39;pengyanjie&#39;; $data[&#39;password&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;); $data[&#39;id&#39;]=array(&#39;lt&#39;,30); $data[&#39;_logic&#39;]=&#39;or&#39;; $list=$user->where($data)->select(); dump($list);
四、复合查询
$user=M(&#39;user&#39;); $data[&#39;username&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;); $data[&#39;password&#39;]=array(&#39;like&#39;,&#39;p%&#39;); $data[&#39;_logic&#39;]=&#39;or&#39;; $where[&#39;_complex&#39;]=$where; $where[&#39;id&#39;]=array(&#39;lt&#39;,30); $list=$user->where($data)->select(); dump($list); //相当于(id<30)and ( (username=pengyanjie) or (password like p%) )
五、统计查询
echo $user->count(); echo &#39;&#39;; echo $user->max(&#39;id&#39;); echo &#39;&#39;; echo $user->where(&#39;id<30&#39;)->min(&#39;id&#39;); echo &#39;&#39;; echo $user->avg(&#39;id&#39;); echo &#39;&#39;; echo $user->sum(&#39;id&#39;);
六、定位查询
$user=new AdvModel(&#39;user&#39;);//实例化高级模型AdvModel //$user=M(&#39;user&#39;,&#39;CommonModel&#39;);//或者将AdvModel用CommonModel来继承 $list=$user->order(&#39;id desc&#39;)->getN(2);//返回结果中的第三条 dump($list); $list=$user->order(&#39;id desc&#39;)->last();//返回最后一条 $list=$user->order(&#39;id desc&#39;)->first();//返回第一条
七、SQL查询
excute()主要用于更新和写入
$Model = new Model() // 实例化一个 model 对象 没有对应任何数据表 $Model->execute( "update think_user set name=&#39;thinkPHP&#39; where status=1" );
query()主要用于查询
$user=M(); $list=$user->query(&#39;select * from aoli_user order by id desc&#39;); dump($list);
八、动态查询
$user=M(&#39;user&#39;); $list=$user->getByusername(&#39;pengyanjie&#39;); $list=$user->getByusername(&#39;pengyanjie&#39;); dump($list); $user=new AdvModel(&#39;user&#39;); $list=$user->top5();//前5条 dump($list);