将查询从不存在的位置转换为Left Join

时间:2017-02-24 10:59:41

标签: mysql laravel laravel-eloquent

我正在使用laravel 5.2,我在转换此查询时遇到问题

 DB::select('SELECT m.*
            FROM member_table AS m
            JOIN treepaths AS t ON m.membershipid = t.descendant
            WHERE m.stage >=' . $usermatrixtype . '
            AND t.ancestor =' . "'" . $firstrightchildmembershipid . "'" . '
            AND NOT EXISTS (
            select user_id from matrix_users AS mr WHERE mr.user_id=m.membershipid AND matrix_id=' . $getusermatrix . '
            ) LIMIT ' . $results
);

这是我试过的

 DB::select('SELECT m.membershipid
            FROM member_table AS m
            JOIN treepaths AS t ON m.membershipid = t.descendant
            WHERE m.stage >=' . $usermatrixtype . '
            AND t.ancestor =' . "'" . $firstleftchildmembershipid . "'" .'
            LEFT OUTER JOIN matrix_users AS mu ON mu.user_id =m.membershipid WHERE
           matrix_id=' . $getusermatrix . ' AND
            mu.userid IS NULL
             LIMIT ' . $results);

它什么都不返回

1 个答案:

答案 0 :(得分:0)

试试这个:

 DB::select('SELECT m.membershipid
            FROM member_table AS m
            JOIN treepaths AS t ON m.membershipid = t.descendant
            LEFT JOIN matrix_users AS mu ON mu.user_id =m.membershipid AND  matrix_id=' . $getusermatrix . '
            WHERE  mu.userid IS NULL AND m.stage >=' . $usermatrixtype . '
            AND t.ancestor =' . "'" . $firstleftchildmembershipid . "'" .'

             LIMIT ' . $results);