查询构造器
简介
Laravel 的数据库查询构造器为创建和运行数据库查询提供了一个方便的接口。它可以用于支持大部分数据库操作,并且在所有支持的数据库系统上统一运行。
获取结果
获取全部数据
你可以使用 DB facade 里的 table 方法来开始查询。table 方法为给定的表返回一个查询构造器实例,允许你在查询上链式调用更多的约束,最后使用 get 方法获取结果:
$users = DB::table('users')->get();
从数据表中获取一条数据
$user = DB::table('users')->where('name', '大郎')->first();
如果你甚至不需要整行数据,则可以使用 value 方法从记录中获取单个值。该方法将直接返回该字段的值:
$email = DB::table('users')->where('name', '大郎')->value('email');
如果是通过 id 字段值获取一行数据,可以使用 find 方法
$user = DB::table('users')->find(3);
获取一列的值
$titles = DB::table('roles')->pluck('title');
foreach ($titles as $v) {
echo $v;
}
聚合函数
查询构造器还提供了各种聚合方法,比如 count,max,min,avg,还有 sum。你可以在构造查询后调用任何方法:
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
当然,你也可以将这些聚合方法与其他的查询语句相结合:
$price = DB::table('orders')
->where('finalized', 1)
->avg('price');
Selects
你可以自定义一个 select 查询语句来查询指定的字段:
$users = DB::table('users')->select('name', 'email as user_email')->get();
原生表达式
有时候你可能需要在查询中使用原生表达式。你可以使用 DB::raw 创建一个原生表达式:
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->get();
Joins
Inner Join 语句
查询构造器也可以编写 join 方法。若要执行基本的「内链接」,你可以在查询构造器实例上使用 join 方法。
$users = DB::table('xsb')
->join('szx', 'xsb.szx', '=', 'szx.id')
->select('xsb.*', 'szx.szx as sname')
->get();
Left Join / Right Join 语句
如果你想使用 「左连接」或者 「右连接」代替「内连接」 ,可以使rightJoin 方法。这两个方法与 join 方法用法相同:
$users = DB::table('xsb')
->leftJoin('szx', 'xsb.szx', '=', 'szx.id')
->select('xsb.*', 'szx.szx as sname')
->get();
Where
简单的 Where 语句
在构造 where 查询实例中,你可以使用 where 方法。调用 where 最基本的方式是需要传递三个参数:第一个参数是列名,第二个参数是任意一个数据库系统支持的运算符,第三个是该列要比较的值。
$users = DB::table('users')->where('age', '=', 18)->get();
为了方便,如果你只是简单比较列值和给定数值是否相等,可以将数值直接作为 where 方法的第二个参数:
$users = DB::table('users')->where('age', 18)->get();
当然,你也可以使用其他的运算符来编写 where 子句:
$users = DB::table('users')
->where('age', '>=', 18)
->get();
$users = DB::table('users')
->where('age', '<>', 18)
->get();
$users = DB::table('users')
->where('name', 'like', '%王%')
->get();
Or 语句
你可以一起链式调用 where 约束,也可以在查询中添加 or 字句。orWhere 方法和 where 方法接收的参数一样:
$users = DB::table('users')
->where('age', '>', 30)
->orWhere('age', '<' , 20)
->get();
如果需要在括号内对 or 条件进行分组,将闭包作为 orWhere 方法的第一个参数也是可以的:
$users = DB::table('users')
->where('name', 'like', "%王%")
->Where(function($query) {
$query->where('age', '>', 30)
->orWhere('age', '<' , 20);
})
->get();
Where 语句特殊用法
whereBetween 方法验证字段值是否在给定的两个值之间:
$users = DB::table('users')
->whereBetween('age', [20, 30])
->get();
whereIn 方法验证给定列的值是否包含在给定数组中:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
whereNull 方法验证指定的字段必须是 NULL:
$users = DB::table('users')
->whereNull('head_img')
->get();
whereDate 方法用于比较字段值与给定的日期(年月日):
$users = DB::table('users')
->whereDate('birthday', '>' , '1989-01-09')
->get();
orderBy
orderBy 方法允许你通过给定字段对结果集进行排序。 orderBy 的第一个参数应该是你希望排序的字段,第二个参数控制排序的方向,可以是 asc 或 desc:
$users = DB::table('users')
->orderBy('age', 'desc')
->get();
如果你需要使用多个字段进行排序,你可以多次引用 orderBy:
$users = DB::table('users')
->orderBy('name', 'desc')
->orderBy('email', 'asc')
->get();
inRandomOrder
inRandomOrder 方法被用来将结果进行随机排序。例如,你可以使用此方法随机找到一个用户:
$randomUser = DB::table('users')
->inRandomOrder()
->first();
groupBy / having
groupBy 和 having 方法用于将结果分组。 having 方法的使用与 where 方法十分相似:
$users = DB::table('users')
->groupBy('szx')
->having('count(szx)', '>', 3)
->get();
limit
要限制结果的返回数量,或跳过指定数量的结果,你可以使用 skip 和 take 方法:
$users = DB::table('users')->skip(10)->take(5)->get();
或者你也可以使用 limit 和 offset 方法:
$users = DB::table('users')
->offset(10)
->limit(5)
->get();