laravel左连接查询不起作用

时间:2017-06-15 18:46:30

标签: php mysql laravel laravel-5.2

我有以下查询

 $data=Post::join('category', 'questions.categoryid', '=', 'category.CategoryId')
         ->where('category.CategoryName','demo')
        ->join('comments', function ($join) {
                $join->on('posts.postId', '=', 'comments.postId')
                ->where('posts.postId', '!=', 'comments.linkId');
        }) ->get();

我有上面的查询,我根据类别名称和连接表评论

退休所有帖子

每个人都认为工作正常,但是后续行无法正常工作。我的意思是即使添加此行仍然可以恢复所有记录,甚至我尝试!=<>但是没有工作

->where('posts.postId', '!=', 'comments.linkId');

1 个答案:

答案 0 :(得分:2)

在JOIN中不使用WHERE子句,可以扩展ON子句:

->join('comments', function ($join) {
      $join->on('posts.postId', '=', 'comments.postId')
           ->on('posts.postId', '!=', 'comments.linkId');
})

这相当于:

JOIN comments ON posts.postId = comments.postId AND posts.postId != comments.linkId
相关问题