Kohana框架数据库操作详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Chaoren666/article/details/53374760
compile //打印sql


插入数据
DB::isnert('表名',array('字段名1','字段名2'))->values(array('值1','值2'))->execute();

查询数据
$restult=DB::select('字段名')->from('表名')->execute(); //返回一个数据对象

$result->as_array(); //用数组输出

DB:select('字段名')->from('表名')->as_object()->execute(); //返回标准对象

DB::select('字段名')->from('表名')->execute()->current(); //返回数组第一行

条件查询
$result=DB::select('字段名')->from('表名')->where('字段名','=','条件')-execute();

$arr1=$result->as_array();

更新数据
$total_rows = DB::update('表名')->set(array('字段名'=>'值'))->where('字段名','=','条件')->execute(); //返回1

删除数据
$total_rows = DB::delete('表名')->where('字段名','=',''值')->execute(); //成功返回1

用sql使某列数值增加
$total_rows = DB::update('users')
->set(array('字段名1'=>DB::expr('字段名1+1')))
->where('字段名','=','条件')
->execute();

Join联查


选择列AS
$arr= DB::select(array('字段名','别名'))->from('表名')->compile(); //打印sql

group_by分组
$arr= DB::select('字段名')->from('表名')->group_by('字段名')->execute();

order_by排序
$arr=DB::select()->from('表名')->order_by('字段名','ASC/DESC')->execute()->as_array();//排序的字段必须是int型

limit截取
$arr=DB::select()->from('表名')->limit('num')->execute()->as_array();

offset
$arr = DB::select()->from('表名')->imit(截取几位)->offset(从哪儿开始)->execute()->as_array();
















猜你喜欢

转载自blog.csdn.net/Chaoren666/article/details/53374760
今日推荐