【问题标题】:Eloquent Laravel multiple like where and where clause雄辩的 Laravel 多重 like where 和 where 子句
【发布时间】:2021-02-13 08:38:48
【问题描述】:

我有下面的 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','LIKE','%OPEN%')
        ->where('al.isOfficeClosed', '=','0');

【问题讨论】:

  • op.Location 上的大写字母可能会导致连接失败?
  • @porloscerrosΨ 不,我试过了。

标签: php laravel eloquent laravel-query-builder


【解决方案1】:

您可以为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','LIKE','%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;

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 2019-01-28
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2015-05-17
    • 2014-01-03
    相关资源
    最近更新 更多