最近剛剛開始學習 thinkphp 這套架構,對於Thinkphp的強大真的是讓哥歎為觀止,其抗壓能力,效能如何這個暫不討論,下面就分享一下 thinkphp 強大的查詢功能,當然這裡只是拋磚引玉而已。
一、帶where條件的普通查詢
1、字串形式
$user=M('user');
$list=$user->where('id>5 and id<9')->select();
$list=$user->where($data)->select();
2、數組形式
$user=M('user');
$list=$user->where(array('username'=>'www.phpernote.com'))->select();
$list=$user->where($data)->select();
3、對象形式
$user=M('user');
$a=new stdClass();
$a->username='www.phpernote.com';
$list=$user->where($a)->select();
4、查詢運算式
EQ 等於
NEQ 不等於
GT 大於
EGT 大於等於
LT 小於
ELT 小於等於
LIKE 等價與sql中的like
[NOT] BETWEEN 查詢區間
[NOT] IN 查詢集合
EXP 指使用標準SQL語句,實現更加複雜的情況
文法格式:$data['欄位名']=array('是運算式','查詢條件');
例如
$data['username']='www.phpernote.com';
實際上是指
$data['username']=array('eq','www.phpernote.com');
$data['username']=array('like','peng%');
$list=$user->where($data)->select();
$data['username']=array(array('like','p%'),array('like','h%'),'or');
$list=$user->where($data)->select();
二、區間查詢
$user=M('user');
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();
三、組合查詢
$user=M('user');
$data['username']='pengyanjie';
$data['password']=array('eq','pengyanjie');
$data['id']=array('lt',30);
$data['_logic']='or';
$list=$user->where($data)->select();
dump($list);
四、複合查詢
$user=M('user');
$data['username']=array('eq','pengyanjie');
$data['password']=array('like','p%');
$data['_logic']='or';
$where['_complex']=$where;
$where['id']=array('lt',30);
$list=$user->where($data)->select();
相當於
(id<30) and ((username=pengyanjie) or (password like p%))
五、統計查詢
echo $user->count();
echo '
';
echo $user->max('id');
echo '
';
echo $user->where('id<30')->min('id');
echo '
';
echo $user->avg('id');
echo '
';
echo $user->sum('id');
六、定位查詢
$user=new AdvModel('user');//執行個體化進階模型AdvModel
//$user=M('user','CommonModel');//或者將AdvModel用CommonModel來繼承
$list=$user->order('id desc')->getN(2);//返回結果中的第三條
$list=$user->order('id desc')->last();//返回最後一條
$list=$user->order('id desc')->first();//返回第一條
七、SQL查詢
excute()主要用於更新和寫入
$Model=new Model() // 執行個體化一個 model 對象,沒有對應任何資料表
$Model->execute("update think_user set name='thinkPHP' where status=1");
query()主要用於查詢
$user=M();
$list=$user->query('select * from aoli_user order by id desc');
dump($list);
八、動態查詢
$user=M('user');
$list=$user->getByusername('pengyanjie');
$list=$user->getByusername('pengyanjie');
$user=new AdvModel('user');
$list=$user->top5();//前5條
您可能感興趣的文章
- thinkphp模板中判斷volist迴圈的最後一條記錄
- thinkphp開發技巧總結
- php中的MVC模式運用技巧
- thinkphp like 查詢
- thinkphp 的 Action 控制器中的系統常量總結
- thinkphp截取中文字串的方法
- thinkphp自動驗證與自動填滿無效的解決辦法
- 使用ThinkPHP必須掌握的調試方法
http://www.bkjia.com/PHPjc/764123.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/764123.htmlTechArticle最近剛剛開始學習 thinkphp 這套架構,對於Thinkphp的強大真的是讓哥歎為觀止,其抗壓能力,效能如何這個暫不討論,下面就分享一下 think...