首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理论- Symfony 3 querybuilder计数()是错误的

理论- Symfony 3 querybuilder计数()是错误的
EN

Stack Overflow用户
提问于 2017-01-18 17:00:29
回答 1查看 327关注 0票数 3

我的数据库中有这样的数据:

我尝试使用foreign_ids的最新created_at值获取guest_identifier。

在这种情况下,我希望:

代码语言:javascript
复制
    foreign_id: 5 for guest_identifier: 12345
    foreign_id: 5 for guest_identifier: 2345
    foreign_id: 4 for guest_identifier: 345

现在,我要计算这些结果并返回如下内容:

代码语言:javascript
复制
[
   {
       "foreign_id": 5,
       "occurrence": 2
   },
   {
       "foreign_id": 4,
       "occurrence": 1
   }
]

我就是这样得到这个结果的:

代码语言:javascript
复制
$qb = $this->createQueryBuilder('statistic')
        ->select('statistic.foreignId, COUNT(statistic.foreignId) as occurrence')
        ->where('statistic.guideId = :guideId')
        ->andWhere('statistic.type = :type')
        ->andWhere('statistic.createdAt BETWEEN :startDate AND :endDate')
        ->groupBy('statistic.guestIdentifier')
        ->setParameters(array(
            'guideId' => $guideId,
            'type' => 'answer_clicked',
            'startDate' => $startDate,
            'endDate' => $endDate
        ))
        ->getQuery();

    $stats = $qb->getResult();

    return $stats;

问题是,我的结果如下:

代码语言:javascript
复制
[
  {
    "foreignId": 5,
    "occurrence": "3"
  },
  {
    "foreignId": 5,
    "occurrence": "3"
  },
  {
    "foreignId": 4,
    "occurrence": "2"
  }
]

我找不到为什么foreign_id: 5的事件是3而不是2,为什么foreign_id: 3是2而不是1。另外,我也不知道如何下次对结果进行分组。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-20 11:20:14

我可以用以下答案来解决我的问题:https://stackoverflow.com/a/28090544/7069057

我的功能现在看起来如下:

代码语言:javascript
复制
$qb = $this->createQueryBuilder('statistic')
        ->select('statistic.foreignId, COUNT(statistic.foreignId)')
        ->where('statistic.guideId = :guideId')
        ->andWhere('statistic.type = :type')
        ->andWhere('statistic.createdAt BETWEEN :startDate AND :endDate')
        ->leftJoin('AppBundle\Entity\Statistic\Statistic', 'stat', Join::WITH,
            'statistic.type = stat.type AND statistic.guestIdentifier = stat.guestIdentifier AND stat.createdAt > statistic.createdAt')
        ->andWhere('stat.createdAt IS NULL')
        ->groupBy('statistic.foreignId')
        ->setParameters(array(
            'guideId' => $guideId,
            'type' => 'answer_clicked',
            'startDate' => $startDate,
            'endDate' => $endDate
        ))
        ->getQuery();

    $stats = $qb->getResult();

    return $stats;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41725244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档