我寻求帮助来编写查询。
一些背景: 我有ff型号
User(users) - id, name
Post (posts) - id, content
Bookmarks - user_id, post_id
Comment( comments) - id, content, post_id
Like(likes) - user_id, comment_id
我想查看哪些用户喜欢帖子中的任何评论,而不为该特定帖子添加书签。
目前我像这样编写查询,但它返回空结果。
Like::leftJoin('comments', 'likes.comment_id', '=', 'comments.id')
->leftJoin('bookmarks', 'bookmarks.post_id', '=', 'comments.post_id')
->whereNull('bookmarks.post_id')
->pluck('likes.user_id')
select likes.user_id from likes
left join bookmarks on bookmarks.post_id = comments.post_id
left join comments on likes.comment_id = comments.id
where bookmarks.post_id is null
我希望它返回没有相应书签的 user_id 列表。 它返回一个空列表。
我无法复制您的问题所建议的连接顺序更改。您还缺少
user_id
和 likes
之间 bookmarks
的连接标准。我想应该是:
Like::join('comments', 'likes.comment_id', '=', 'comments.id')
->leftJoin('bookmarks', function ($join) {
$join->on('bookmarks.post_id', '=', 'comments.post_id');
$join->on('bookmarks.user_id', '=', 'likes.user_id');
})
->whereNull('bookmarks.post_id')
->distinct()
->pluck('likes.user_id');