【问题标题】:Can not manage Laravel query builder无法管理 Laravel 查询生成器
【发布时间】:2015-02-23 12:59:46
【问题描述】:
$a = 0;
$searchqueryMake = 'whereHas';
foreach ($options as $param)
{
    if($a>0 && $searchCriteria == 2)
    {
          $searchqueryMake = 'orWhereHas';
          $u = ($u->$searchqueryMake('option', function($q) use($param){
                $q->where('option_id', '=', $param );
          }));
    }
    else
    {
         $u = ($u->$searchqueryMake('option', function($q) use($param){
                 $q->where('option_id', '=', $param );
         }));
    }
$a++;
}

$a = 0;
$searchqueryMake = 'whereHas';
foreach ($specific_University as $param)
{
    if($a>0 && $searchCriteria == 2)
    {
        $searchqueryMake = 'orWhereHas';
        $u = $u->$searchqueryMake('degree', function($q) use($param){
            $q->whereHas('university', function($q) use($param){
                $q->where('id', '=', $param);
            });
        });
    }
    else
    {
        $u = $u->$searchqueryMake('degree', function($q) use($param){
            $q->whereHas('university', function($q) use($param){
                $q->where('id', '=', $param);
            });
        });
    }
$a++;
}

我已经编写了这段代码来获取低于 sql 结果 对于 AND 条件 sql 结果将是这个-

    select * from `users` where `group_id` = ? and
 (select count(*) from `options` inner join `user_option` on `options`.`id` = `user_option`.`option_id` where
 `user_option`.`user_id` = `users`.`id` and
 `option_id` = ?) >= 1 and
 (select count(*) from `options` inner join `user_option` on `options`.`id` = `user_option`.`option_id` where
 `user_option`.`user_id` = `users`.`id` and `option_id` = ?) >= 1 and
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and
 `id` = ?) >= 1) >= 1 and
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and
 `id` = ?) >= 1) >= 1 and
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and
 `id` = ?) >= 1) >= 1

当用户选择或条件时,sql结果再次是 -

    select * from `users` where `group_id` = ? and 
(select count(*) from `options` inner join `user_option` on `options`.`id` = `user_option`.`option_id` where
 `user_option`.`user_id` = `users`.`id` and
 `option_id` = ?) >= 1 or
 (select count(*) from `options` inner join `user_option` on `options`.`id` = `user_option`.`option_id` where
 `user_option`.`user_id` = `users`.`id` and `option_id` = ?) >= 1 and
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and `id` = ?) >= 1) >= 1 or
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and
 `id` = ?) >= 1) >= 1 or
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and
 `id` = ?) >= 1) >= 1

最上面的查询很好,因为所有条件都在 AND 中。但是最下面的一个没有得到正确的值,因为我们需要所有的条件 AND 和 OR 并且还需要用括号分隔以获得正确的结果,而使用这种雄辩的方法我无法做到这一点。谁能帮我找出这个问题...

确切的查询是我需要的 -

select * from `users` where `group_id` = ? and 
((select count(*) from `options` inner join `user_option` on `options`.`id` = `user_option`.`option_id` where
 `user_option`.`user_id` = `users`.`id` and
 `option_id` = ?) >= 1 or
 (select count(*) from `options` inner join `user_option` on `options`.`id` = `user_option`.`option_id` where
 `user_option`.`user_id` = `users`.`id` and `option_id` = ?) >= 1) and
 ((select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and `id` = ?) >= 1) >= 1 or
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and
 `id` = ?) >= 1) >= 1 or
 (select count(*) from `degrees` inner join `user_degree` on `degrees`.`id` = `user_degree`.`degree_id` where
 `user_degree`.`user_id` = `users`.`id` and
 (select count(*) from `universities` where
 `degrees`.`university_id` = `universities`.`id` and
 `id` = ?) >= 1) >= 1))

【问题讨论】:

    标签: php laravel laravel-4 laravel-3


    【解决方案1】:

    好吧,这是一个非常庞大的查询,老实说,我没有时间详细讨论它。但这里是你如何在基本上每个 where 条件周围都用括号括起来的方法。这称为嵌套在哪里

    首先,这里有几个正常的 where:

    where('foo', 'foo')->orWhere('foo', 'bar')->where('bar', 'bar')
    

    这会导致:

    WHERE foo = 'foo' OR foo = 'bar' AND bar = 'bar'
    

    执行如下:

    WHERE foo = 'foo' OR ( foo = 'bar' AND bar = 'bar' )
    

    要改变这一点,我们可以添加一个带闭包的 where:

    where(function($query){
        $query->where('foo', 'foo');
        $query->orWhere('foo', 'bar');
    })->where('bar', 'bar');
    

    现在 SQL 看起来像这样:

    WHERE ( foo = 'foo' OR foo = 'bar' ) AND bar = 'bar'
    

    我希望您可以使用它并相应地更改您的代码。

    【讨论】:

      猜你喜欢
      • 2014-04-10
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2015-01-12
      • 1970-01-01
      • 2016-01-10
      相关资源
      最近更新 更多