【发布时间】: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