2014-12-26 128 views
0
$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结果 并条件SQL结果将是这个 -无法管理Laravel查询生成器

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)) 

回答

1

好吧,这是一个相当庞大的查询和我真的没有时间去仔细阅读它。但是,这里是你如何绕着基本上每个条件包围括号。这就是所谓的嵌套其中

首先,这里有几个正常何在:

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(function($query){ 
    $query->where('foo', 'foo'); 
    $query->orWhere('foo', 'bar'); 
})->where('bar', 'bar'); 

现在的SQL是这样的:

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

我希望你能与工作,并相应地改变你的代码。

+0

谢谢@lukasgeiter它对我来说工作正常 –