Laravel DB子查询并在子查询中选择

时间:2017-06-09 10:53:38

标签: laravel

SELECT mc.pr_category_id, mc.parents, mc.level, mc.lft, mc.rgt, mcdscr.cat_name FROM table_cat as mc
RIGHT JOIN
    ( SELECT c.*, dscr.cat_name FROM table_cat AS c 
        JOIN table_cat_description AS dscr ON c.pr_category_id = dscr.pr_category_id AND c.pr_category_id = $pr_category_id 
    ) AS test ON (mc.rgt < test.rgt) AND mc.lft > test.lft
JOIN table_cat_description AS mcdscr ON mc.pr_category_id = mcdscr.pr_category_id 
ORDER BY mcdscr.cat_name ASC

我正在使用leftJoin但没有工作。你能帮助子查询吗?

2 个答案:

答案 0 :(得分:0)

没有经过测试......但是想法应该在某处。

$sql1 = DB::table(DB::raw('SELECT c.*, dscr.cat_name FROM table_cat AS c JOIN table_cat_description AS dscr ON c.pr_category_id = dscr.pr_category_id AND c.pr_category_id = $pr_category_id 
    '));
$sql2 = DB::table(DB::raw('SELECT mc.pr_category_id, mc.parents, mc.level, mc.lft, mc.rgt, mcdscr.cat_name'));

$query = DB::table(DB::raw("($sql) AS mc"))->leftJoin(DB::raw('($sql2) as test'), function($join)
        {
        $join->on('mc.rgt', '<', 'test.rgt');
        $join->on('mc.lft', '>', 'test.lft');
        })
    ->join('table_cat_description AS mcdscr', 'mc.pr_category_id', '=', 'mcdscr.pr_category_id')
    ->orderBy('mcdscr.cat_name', 'asc')
    ->get();

答案 1 :(得分:0)

mysql> select * from employees;
Empty set (0.00 sec)

我正在使用查询和工作。非常感谢你