在很多時候,我們會遇到數據庫表里面的某個值需要+1操作,我們不能簡單地在update的時候寫入array('key' => 'key+1'),因為在解析sql的時候,key+1 會帶上引號作為一個字符串被處理,所以,這樣的操作並沒有達到我們想要的效果,當然,這是有解決方法的。具體操作如下:
1、加操作:DI()->notorm->user->where('id', 1)->update(array('sum' => new NotORM_Literal("sum + 1")));
2、group:DI()->notorm->user->select('name , count(*) as count')->group('name')->fetchAll();
3、快速函數sum,count,max,min
return DI()->notorm->user->sum('id'); //做加法
return DI()->notorm->user->max('id'); //獲取這個key中最大的值
return DI()->notorm->user->min('id'); //獲取這個key中最小的值
return DI()->notorm->user->count(); //統計一共幾條數據
4、批量插入insert_multi()
$rows = array(
array('name' => 'A君', 'age' => 12, 'note' => 'AA'),
array('name' => 'B君', 'age' => 14, 'note' => 'BB'),
array('name' => 'C君', 'age' => 16, 'note' => 'CC'),
);
$rs = $user->insert_multi($rows);
