问题描述
select * from operatories op
left join locations al on op.location = al.location
where op.opname like '%NP%' or op.opname like '%OPEN%'
and al.isOfficeClosed = 0;
这可行,我得到了我期望的数据,但是我试图在Laravel查询生成器中编写该数据,但它没有选择最后一个and
al.isOfficeClose = 0。看到我做错了吗?
Laravel:
$locs = DB::table('operatories as op')
->leftJoin(DB::raw('locations al'),function($join) {
$join->on('op.Location','=','al.location');
})
->Where('op.opname','LIKE','%NP%')
->orWhere('op.opname','%OPEN%')
->where('al.isOfficeClosed','0');
解决方法
您可以为where
子句定义一个函数,如下所示:
$locs = DB::table('operatories as op')
->leftJoin(DB::raw('locations al'),function($join) {
$join->on('op.Location','=','al.location');
})
->where(function($q) {
$q->where('op.opname','LIKE','%NP%');
$q->orWhere('op.opname','%OPEN%');
})
->where('al.isOfficeClosed','0');
顺便
select * from operatories op
left join locations al on op.location = al.location
where op.opname like '%NP%' or op.opname like '%OPEN%'
and al.isOfficeClosed = 0;
不同于(请注意括号)
select * from operatories op
left join locations al on op.location = al.location
where (op.opname like '%NP%' or op.opname like '%OPEN%')
and al.isOfficeClosed = 0;