在数据查询过程中会经常用到数据求和、求平均值、最大值、最小值、字段合并等功能,下面为您逐一介绍。
功能 : 获取数据条目数
参数 : 汇总字段,可选参数,默认 *
演示 :
$res = database::table('students') ->where('st_id < ?', array(100)) ->order('st_class_id asc') ->count(); echo "数据总数 : {$res} <br />"; $res = database::table('students') ->count(); echo "数据总数 : {$res} <br />";
功能 : 数据求和
参数 : 求和字段
演示 :
$res = database::table('students') ->where('st_id < ?', array(100)) ->sum('st_age'); echo "年龄和 : {$res} <br />";
功能 : 数据求平均值
参数 : 求平均值字段
演示 :
$res = database::table('students') ->where('st_id < ?', array(100)) ->avg('st_age'); echo "平均年龄 : {$res} <br />";
功能 : 数据求最大值
参数 : 求最大值字段
演示 :
$res = database::table('students') ->where('st_id < ?', array(100)) ->max('st_age'); echo "最大年龄 : {$res} <br />";
功能 : 数据求最小值
参数 : 求最小值字段
演示 :
$res = database::table('students') ->where('st_id < ?', array(100)) ->min('st_age'); echo "最小年龄 : {$res} <br />";
功能 : mysql原生函数用法,通过 fetch 或者 fetchAll 设置字段部分
$res = database::table('students') ->where('st_id < ?', array(10)) ->fetchAll('CONCAT(st_name,"-",st_age) as concat_val, st_id'); print_r($res);
功能 : mysql原生函数用法,通过 where 设置条件
$res = database::table('students') ->where('st_id < ?', array(10)) ->and('st_id % 2 = 0') ->fetchAll(); print_r($res);