从数据库获取趋势帖子

编程入门 行业动态 更新时间:2024-10-28 20:19:20
本文介绍了从数据库获取趋势帖子 - symfony 查询构建器 - MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试进行一个查询,该查询必须组合来自 2 个不同表的信息,但我在逻辑上遇到了问题.

I'm trying to make a query which has to combine information from 2 different tables and I'm having trouble with the logic.

这是我的表格,我排除了一些不相关的信息

Here are my tables,I'm excluding some of the irrelevant info

post(groupId, dateCreated)

post(groupId, dateCreated)

postbump(userId, postId)

postbump(userId, postId)

我正在尝试获取热门"帖子.换句话说,我需要获得

I'm trying to get Posts which are "trending". In other words, I need to get the posts which are

  • 在给定的组中,(由 $groups 数组提供)

  • In the given groups,(provided by $groups array)

    在给定日期之后,(由 $date 提供)

    After a given date,(provided by $date)

    按他们的颠簸数量排序

    这是我正在构建的功能,它只会获取新帖子,但它可能有助于了解我所追求的内容.请注意,我拥有的代码的当前存储库是 post 存储库(对于那些不熟悉 symfony 查询构建器的人,它基本上意味着它会自动从 post 表中选择).

    This is my function that I am building off of which only gets new posts but it may help provide an understanding of what I am after. Note that the current repository of the code I have is the post repository(for those not familiar with symfony query builder it basically means that it automatically selects from the post table).

    public function getNewHomeFeedPosts($groups){ $qb = $this->repository->createQueryBuilder('x'); $qb->select('x'); $qb->where('x.groupId IN (:groups)'); $qb->setParameter('groups',$groups); $qb->orderBy('x.dateCreated','DESC'); return $qb->getQuery()->getResult(); }

    这是我到目前为止所拥有的,我真正的问题在于按每个帖子的颠簸数量排序.我认为在某些时候必须使用连接.

    This is what I have so far, My real issue lies in ordering by the amount of bumps each post has. I'm thinking that a join is going to have to be utilized at some point.

    public function getTrendingHomeFeedPosts($groups, $date){ $qb= $this->repository->createQueryBuilder('x'); $qb->select('x'); $qb->where('x.groupId IN (:groups) AND x.dateCreated < :dateCreated'); $qb->setParameter('groups',$groups); $qb->setParameter('dateCreated', $date); //todo return $qb->getQuery()->getResult(); }

    如果您不熟悉 querybuilder 语法,那么 SQL 中的答案就可以了.

    If you aren't familiar with querybuilder syntax then an answer in SQL would be fine.

    推荐答案

    我不懂 symfony,所以我的答案是在 sql 中.您需要加入post 和postbump 表,按postid 分组并计算碰撞的数量并过滤分组和日期:

    I do not know symfony, so my answer is in sql. You need to join the post and postbump tables, group by postid and count the number of bumps and filter on groups and the date:

    select p.postid, p.groupid, p.datecreated, count(*) as postbumps from post p left join postbump pb on p.postid=pb.postid where p.groupid in (...) and p.datecreated<... group by p.postid, p.groupid, p.datecreated order by count(*) desc

    还请注意,您不能简单地将 in 运算符中的参数数组绑定为单个值 - 除非 symfony 的查询构建器使用字符串操作进行参数设置而不是准备好的语句.

    Pls also note that you cannot simply bind an array of parameters within the in operator as a single value - unless symfony's query builder uses string operations for parameter setting and not prepared statements.

  • 更多推荐

    从数据库获取趋势帖子

    本文发布于:2023-10-23 23:18:50,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1522299.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:趋势   数据库   帖子

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!