Laravel查询构建器:添加另一个加入会导致错误的计数结果
当我不使用特定的加入时,我有一个查询,可以返回正确的结果。但是,当我添加另一个加入时,计数显示出完全不同的结果。我不明白为什么会发生这种情况。
这是我的查询:
$query = DB::table('contentable_tips')
->join('content', 'contentable_tips.content_id', '=', 'content.id')
->join('users', 'content.author_id', '=', 'users.id')
->join('contentable_subcategories', 'content.parent_id', '=', 'contentable_subcategories.content_id')
->join('content as subcategory', 'contentable_subcategories.content_id', '=', 'subcategory.id')
->join('content as category', 'subcategory.parent_id', '=', 'category.id')
->join('content as comment_content', 'contentable_tips.content_id', '=', 'comment_content.parent_id')
->join('contentable_comments', 'comment_content.id', '=', 'contentable_comments.content_id')
->groupBy('contentable_tips.content_id');
$query = $query->select(
'category.slug as parent_slug',
'content.id',
'contentable_tips.title',
'contentable_tips.name',
'contentable_tips.likes_count',
'contentable_tips.content',
'contentable_subcategories.content_id',
'contentable_subcategories.title as subject',
'content.author_id',
'users.public as author_public',
'users.name as author_name',
DB::raw('count(contentable_comments.id) as comment_count'),
)
->groupBy('contentable_subcategories.id')
->orderBy('likes_count', 'desc')
->limit($limit)
->get();
但是,当我添加此加入时:
->join('content_likes', function ($join) {
$join->on('content_likes.content_id', '=', 'contentable_tips.content_id')
->where('content_likes.created_at', '>=', now()->subMonth());
})
注释计数从20到800更改。这显然不正确,我还在数据库中检查了此信息。我想根据过去一个月给出的喜欢添加likes_count。我还删除了contentable_tips.likes_count
上的选择,因为当我从content_likes
表中获得类似计数时,不再需要。这就是为什么我添加了此特定的加入。
我已经尝试在content_likes
之前加入content
表,但这无效。我只是不明白为什么添加所有这些加入并不是问题,但是当我添加此加入时,它突然显示出错误的结果。
如果需要更多信息,我将提供。
I have a query which returns the right results when I don't use a specific join. But when I add another join, the count shows completely different results. I don't understand why this is happening.
This is my query:
$query = DB::table('contentable_tips')
->join('content', 'contentable_tips.content_id', '=', 'content.id')
->join('users', 'content.author_id', '=', 'users.id')
->join('contentable_subcategories', 'content.parent_id', '=', 'contentable_subcategories.content_id')
->join('content as subcategory', 'contentable_subcategories.content_id', '=', 'subcategory.id')
->join('content as category', 'subcategory.parent_id', '=', 'category.id')
->join('content as comment_content', 'contentable_tips.content_id', '=', 'comment_content.parent_id')
->join('contentable_comments', 'comment_content.id', '=', 'contentable_comments.content_id')
->groupBy('contentable_tips.content_id');
$query = $query->select(
'category.slug as parent_slug',
'content.id',
'contentable_tips.title',
'contentable_tips.name',
'contentable_tips.likes_count',
'contentable_tips.content',
'contentable_subcategories.content_id',
'contentable_subcategories.title as subject',
'content.author_id',
'users.public as author_public',
'users.name as author_name',
DB::raw('count(contentable_comments.id) as comment_count'),
)
->groupBy('contentable_subcategories.id')
->orderBy('likes_count', 'desc')
->limit($limit)
->get();
But when I add this join:
->join('content_likes', function ($join) {
$join->on('content_likes.content_id', '=', 'contentable_tips.content_id')
->where('content_likes.created_at', '>=', now()->subMonth());
})
the comment count changes for example from 20 to 800. This obviously isn't correct, I also checked this in my database. I want to add the likes_count based upon the likes given in the past month. I also removed the select on contentable_tips.likes_count
as it is not needed anymore when I get the like count from the content_likes
table. That is why I added this specific join.
I already tried joining the content
table before the content_likes
but that didn't work. I just don't understand why adding all these joins wasn't a problem, but when I add this join it suddenly shows wrong results.
If any more information is needed, I will provide it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法找到了一个解决方案。我添加了一个原始查询,其中我根据create_at日期计数喜欢的来计数:
I managed to find a solution. I added a raw query where I count the likes based on the created_at date: