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它对我来说工作正常 –