mysql - 如何使用 Laravel 查询生成器在 WHERE 条件周围添加括号

标签 mysql laravel

我正在使用 Laravel 查询构建器根据用户的过滤器选择动态过滤数据:

 $query = DB::table('readings');
 foreach ($selections as $selection) {
   $query->orWhere('id', $selection);
 }
 $query->whereBetween('date', array($from, $to));
 $query->groupBy('id');

当我检查 SQL 时,我得到如下信息:

select count(*) as `count` from `readings` where `id` = 1 or id` = 2 and `date` between "2013-09-01" and "2013-09-31" group by `id`;

但我需要的是这样的(在 or 语句周围有括号):

select count(*) as `count` from `readings` where (`id` = 1 or id` = 2) and `date` between "2013-09-01" and "2013-09-31" group by `id`;

如何使用 Laravel 查询构建器在 WHERE 条件周围添加括号?

最佳答案

非常有用,我用这个:

->where(function ($query) use ($texto){
    $query->where('UPPER(V_CODIGO)', 'LIKE', '%'.Str::upper($texto).'%')
          ->orWhere('UPPER(V_NOMBRE)', 'LIKE', '%'.Str::upper($texto).'%');
});

关于mysql - 如何使用 Laravel 查询生成器在 WHERE 条件周围添加括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22694866/

相关文章:

mysql - 在 INSERT INTO 语句 MySQL 中引用当前行

php - mysql_query 没有按预期工作

php - 在 laravel 中按列获取所有条目的总和

php - 如何知道 MySQL 何时需要转义

php - Laravel 5.2 - 方法链接不存在

javascript - 我想在我的 ajax 回调函数中设置一个 url

php - laravel 存储过程错误

php - 如何从 Laravel 5 中的模块/目录获取路由列表?

php - 多个 ng-options 下拉菜单无法正常工作?

php - 如何进行受集合约束的查询