版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cofecode/article/details/83104465
get() //从数据表中取得所有的数据列
first() //取一条数据列
where() //写条件
select() // 查询部分字段
chunk() // 分块查询
pluck() // 取某字段值
lists() // 取某字段值,且可以自定义键值
get()
得到一个数组
$re = DB::table('user_ali') -> get();
dd($re);
first()
$re = DB::table('user_ali') -> first();
// 此时就是取第一条记录
$re = DB::table('user_ali') -> where('user_id',5) -> first();
// 取user_id 为5的一列
DB::table('user_ali') -> orderBy('user_id','desc') -> first();
// user_id 按降序 取第一列
where()
DB::table('user_ali') -> where('user_id','>','110') -> get();
// user_id 按降序 取第一列
pluck()
DB::table('user_ali') -> pluck('ali_user');
array:27 [▼
0 => "123123"
1 => ""[email protected]""
2 => ""13802251236155""
3 => ""[email protected]""
4 => ""[email protected]""
5 => ""15812312374824468""
6 => ""15874812324468""
]
lists()
DB::table('user_ali') -> pluck('ali_user');
DB::table('user_ali') -> lists('ali_user');
// 这两者效果是一样的
不同的是,lists() 还可以返自定义的键值。
DB::table('user_ali') -> lists('ali_user','user_id');
将user_id 作为键值返回
array:27 [▼
6 => ""13802324234324""
8 => ""[email protected]""
9 => ""[email protected]""
]
select() 查询指定字段
DB::table('user_ali') -> get();
// 只查两个字段。
DB::table('user_ali') -> select('user_id','ali_user') -> get();
chunk()
从数据表中分块查找数据列
数据库内容很多时,比如几百万条时。
echo '<pre>';
DB::table('user_ali') -> chunk(2,function($re) {
var_dump($re);
});
聚合函数
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');
注意:max(‘price’) 返回的是最大的price的那个值,并非那条数据。
sum() 某一列的和