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))
謝謝@lukasgeiter它對我來說工作正常 –