Php laravel中两次联接表的语法
我正在尝试加入以下三个表。问题在于我想使用'AND'运算符将posts.id连接到shares.post_id的部分,这是不正确的。正确的语法是什么Php laravel中两次联接表的语法,php,mysql,Php,Mysql,我正在尝试加入以下三个表。问题在于我想使用'AND'运算符将posts.id连接到shares.post_id的部分,这是不正确的。正确的语法是什么 $sharedPosts = DB::table('shares') ->join('users', 'users.id', '=', 'shares.user_id') ->join('posts', 'posts.user_id', '=', 'users.id' , 'AND' ,
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', 'posts.user_id', '=', 'users.id' , 'AND' , 'posts.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
尝试使用别名:
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts as p1', 'p1.user_id', '=', 'users.id' , 'AND' , 'p1.id', '=', 'shares.post_id')
->join('posts as p2', 'p2.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
不能在没有别名的情况下两次联接表
虽然如果您的join中有两个子句,但我认为您应该按照
编辑(有一个帖子加入):
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', , function($join){
$join->on('posts.user_id', '=', 'users.id')
->on('posts.id', '=', 'shares.post_id')
})
->where('shares.user_id', '=', $myFriends)
->get();
很抱歉,以下内容不应该出现在查询中:->join('posts','posts.id','=','shares.post_id'),我已经编辑过。你现在可以检查一下吗?我想第3行中有一个额外的逗号,谢谢Aloties,删除了它:)
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', , function($join){
$join->on('posts.user_id', '=', 'users.id')
->on('posts.id', '=', 'shares.post_id')
})
->where('shares.user_id', '=', $myFriends)
->get();