雄辩的Laravel多个,如where和where子句

问题描述

我有下面的MySQL查询有效。

MysqL

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;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...