Laravel查询构建器:添加另一个加入会导致错误的计数结果

发布于 2025-01-23 06:13:18 字数 2270 浏览 0 评论 0原文

当我不使用特定的加入时,我有一个查询,可以返回正确的结果。但是,当我添加另一个加入时,计数显示出完全不同的结果。我不明白为什么会发生这种情况。

这是我的查询:

 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2025-01-30 06:13:18

我设法找到了一个解决方案。我添加了一个原始查询,其中我根据create_at日期计数喜欢的来计数:

->leftJoin(DB::raw('(SELECT content_id, count(id) as likes FROM content_likes WHERE created_at >= now() - interval 1 month group by content_id) as content_likes'),'content_likes.content_id', '=', 'contentable_tips.content_id')

I managed to find a solution. I added a raw query where I count the likes based on the created_at date:

->leftJoin(DB::raw('(SELECT content_id, count(id) as likes FROM content_likes WHERE created_at >= now() - interval 1 month group by content_id) as content_likes'),'content_likes.content_id', '=', 'contentable_tips.content_id')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文