Laravel 5.1嵌套复杂查询

时间:2017-04-22 10:34:39

标签: php mysql laravel

我正在尝试从几个小时开始查询,但我无法用laravel语法进行此查询

\DB::select(\DB::raw(
"
SELECT * FROM products AS P WHERE (`d` = $d)
            AND (`mis` BETWEEN $min_c AND $max_c)
            AND (`e` BETWEEN $e_min AND $e_max)
            AND p1 IN
                            (
                            SELECT p1
                            FROM autos AS A
                            WHERE (`ma` = $strma)
                            AND (`d` = $d )
                            AND (`mis` BETWEEN $min_c AND $max_c)
                            AND (`e` BETWEEN $e_min AND $e_max)
                            AND (`p1` = P.p1 )
                            AND (`p2` = P.p2 )
                            )
            AND p2 IN
                            (
                            SELECT p2
                            FROM autos AS A
                            WHERE (`ma` = $strma)
                            AND (`d` = $d )
                            AND (`mis` BETWEEN $min_c AND $max_c)
                            AND (`e` BETWEEN $e_min AND $e_max)
                            AND (`p1` = P.p1 )
                            AND (`p2` = P.p2 )
                            )
"             
));

我想我必须使用像

这样的2个功能
->whereIn(array('p1','p2'),
function($query){

...
}
function($query2){
...
}
)

但它不起作用 我可以用laravel语法以什么方式向数据库询问这个?

1 个答案:

答案 0 :(得分:0)

我用2个函数解决了它的问题。

->whereIn('p1',function($query) use (...
->whereIn('p2',function($query2) use (...