2014-12-26 129 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它對我來說工作正常 –