RT.
ThinkPHP常規的CRUD操作總結如下:
$user->find(); //找到一個複合搜索條件的數據項
$user->getField();//返回參數限定的數據表中的數據項
$user->where('id=1')->select(); //返回符合where條件的數據項,這裏的條件可以是模糊查詢like,也可以是精確查詢
$where['id']=1; //精確條件
$where['username']=array('like','%alex%');//模糊條件
$user->where($where)->select(); //以數組作爲條件進行查詢
$user->count(); //計算數量
$user->max();
$user->query('select * from XXX'); //使用sql語句進行查詢
$user->execute('insert update delete');
$user->getByXX();//通過數據項字段來查找,這個用着最爽
除了最簡單的查詢之外,ThinkPHP還支持組合查詢和區間查詢,表連接等數據庫常見的操作。
組合查詢,舉個例子:
$where['id'] = array('gt', 4);
$where['_logic']='or';
$where['name'] = array('like', '%giant%');
$map['_complex']=$where;
$map['$active']=1;
//等同於 (active =1 and (username like '%giant' or id >1))
區間查詢,舉個例子:
$where['id']= array(array('gt',1), array('lt',10));
排序分組處理,舉個例子:
$user->order('id desc')->select();
$user->order('id')->limit(5)->select();
$user->page('1,10')->select();
$user->group('active')->select();
$user->group('active')->having('active=1')->select();
表連接,默認的表連接是左連接:
//表連接 默認左連接
$user->join('A on user.id=A.id')->select();
$user->join('inner A on user.id=A.id')->select(); //內連接
$user->join('right A on user.id=A.id')->select();//有鏈接
$user->lock(true)->select();//數據鎖
以上就是常用的數據庫操作。