ThinkPHP學習劄記(九)Thinkphp中的查詢方式大綜合

來源:互聯網
上載者:User
ThinkPHP學習筆記(九)Thinkphp中的查詢方式大綜合
where($data)->select(); * 3.對象形式 * $user=M('user'); * $condition=new stdClass(); * $condition->username='user'; * $list=$user->where($condition)->select(); * 判斷條件:(太多)查詢運算式:5.3. *eq(相等)、ne(不相等)、neq(不相等)、gt(大於)、lt(小於)、elt(小於等於)、 *le(小於等於)、gte(大於等於)、ge(大於等於)、 *like *[not] between *[not] in *exp 執行標準sql語句 * 區間查詢 * 組合查詢 * 複合查詢 * 統計查詢 * 定位查詢 * sql查詢 * 動態查詢 * */class SelectAction extends Action{public function index(){   $user=M('user');////1.1數組查詢(可以使用查詢運算式來設定擷取範圍)////DB_LIKE_FIELDS=>'username,password';(自動進行模糊比對)//   $data['username']=array('like','us%');//   $data['id']=array('between','0,10');//   $data['id']=array('not between',array('5','10'));//   $data['id']=array('in','5,8,10');//   $list=$user->where($data)->select();//$date['id']=array('exp','in(0,2,3,7)');//$date['createtime']=array('exp','createtime+1');//   $list=$user->where(array('id'=>1))->save($date);//   dump($list);//   //1.2對象形式//   $user=M('user');//   $condition=new stdClass();//   $condition->username='user';//   $list=$user->where($condition)->select();//   dump($list); //2區間查詢//$data['id']=array(array('gt',3),array('lt',10));//$data['id']=array(array('gt',3),array('lt',10),'or');//$data['id']=array(array('gt',3),array('lt',10),'and');//$data['username']=array(array('like','u%'),array('like','a%'),'or');//   $list=$user->where($data)->select();//   dump($list);   //3.組合查詢//   $data['username']='user';//   $data['id']='8';//   $data['_logic']='or';//條件為or//   $data['_string']='id<5';//就是在sql後面加入標準的sql條件//   $list=$user->where($data)->select();//   dump($list);   //4.複合查詢(id<5 and (username=''or createtim=''))//   $data['username']=array('eq','user');//   $data['createtime']=array('eq','0');//   $data['_logic']='or';//條件為or//   $where['_complex']=$data;//   $where['id']=array('lt',2);//   $list=$user->where($where)->select();//   dump($list);   //5.統計查詢count\max\min\avg\sum//   $list=$user->where('id>5')->count('id');//   dump($list);//   $list=$user->max('createtime');//   dump($list);//   $list=$user->min('createtime');//   dump($list);//   $list=$user->avg('createtime');//   dump($list);//   $list=$user->sum('createtime');//   dump($list);   //6.定位查詢getN();first();last();//   //6.1初始化////   $user=new AdvModel('user');////6.1.2或者new一個ComModel繼承AdvModel//   $user=new AdvModel('user');//   //直接返回結果當中的某條記錄從0開始//   $list=$user->getN(0);//   dump($list);   //7.sql查詢execute(更新和寫入;有受影響行數無結果集)、query(用於查詢)//   $list=$user->query('from tb_user order by id desc');//   dump($list);   //8.動態查詢(方法後加上欄位名getByUsername或者查詢top)   $user=new AdvModel('user');//   $list=$user->getByUsername('user');   $list=$user->top3();   dump($list);}}?>

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.