laravel 查询构造器2

//查询构造器
public function query()
{
//获取所有的数据
$student = DB::table('student')->get();
var_dump($student);

//获取一条数据
$student = DB::table('student')->first();
var_dump($student);

//使用where获取数据
$student = DB::table('student')->where('id','>=',12)->get();
var_dump($student);

//使用多个where条件获取数据
$student = DB::table('student')
->whereRaw('id>=? and age > 10',[2,18])
->get();
var_dump($student);

//pluck返回结果集中指定的字段,列数据
$student = DB::table('student')
->whereRaw('id>=? and age > 10',[2,18])
->pluck('name');
var_dump($student);

//lists,返回结果
$student = DB::table('student')
->whereRaw('id>=? and age > 10',[2,18])
->lists('name','id');//id作为下标,name作为值
var_dump($student);

//select获取指定的字段
$student = DB::table('student')
->whereRaw('id>=? and age > 10',[2,18])
->select('id','name','age')
->get();
var_dump($student);

//chunk,分次查询,查询量过大的时候使用
DB::table('student')->chunk(1000,function($students){
var_dump($students);//每次查询1000条
// if(条件满足){
// return false;//跳出查询
// }
});

//查询中的聚合函数
//count() avg() max() sum() min()
$count = DB::table('student')->count();
$avg = DB::table('student')->avg('age');
$max = DB::table('student')->max('age');
$min = DB::table('student')->min('age');
$sum = DB::table('student')->sum('age');

}
原文地址:https://www.cnblogs.com/gyfluck/p/9039678.html