Doctrine orm 使用查询生成器在单个查询中返回平均值

Doctrine orm 使用查询生成器在单个查询中返回平均值,doctrine-orm,query-builder,Doctrine Orm,Query Builder,好的,我想返回查询生成器中的平均事务大小。这绝对不是我的强项,因此希望我能在这方面找到一些帮助: 这就是我所拥有的: 我需要得到总交易额,即100美元 我需要得到交易的数量,即10 上述平均交易价值为10美元 现在在查询生成器中执行此操作: 步骤1:获取总数: $qb = $this->transactionRepository->createQueryBuilder('u'); $qb ->add('select','SUM(u.amount)')

好的,我想返回查询生成器中的平均事务大小。这绝对不是我的强项,因此希望我能在这方面找到一些帮助:

这就是我所拥有的:

我需要得到总交易额,即100美元 我需要得到交易的数量,即10 上述平均交易价值为10美元

现在在查询生成器中执行此操作:

步骤1:获取总数:

    $qb = $this->transactionRepository->createQueryBuilder('u');

    $qb ->add('select','SUM(u.amount)')
        ->where('u.status = :status')
        ->andWhere('u.type = :type')
        ->andWhere('u.identity = :identity')
        ->setParameter('status' , 1)
        ->setParameter('type' , 1)
        ->setParameter('identity' , 1);

    $total = $qb
        ->getQuery()
        ->getSingleScalarResult();
步骤2,获取总交易记录:

    $qb = $this->transactionRepository->createQueryBuilder('u');

    $qb ->add('select','COUNT(u.amount)')
        ->where('u.status = :status')
        ->andWhere('u.type = :type')
        ->andWhere('u.identity = :identity')
        ->setParameter('status' , 1)
        ->setParameter('type' , 1)
        ->setParameter('identity' , 1);

    $transaction_count = $qb
        ->getQuery()
        ->getSingleScalarResult();
步骤3:要获得平均值:

$total/$transaction\u count


所以我的问题是,是否可以在一个查询中执行此操作?

好的,这比我想象的要简单:

$qb = $this->transactionRepository->createQueryBuilder('u');

    $qb ->add('select','SUM(u.total)/COUNT(u.amount)')
        ->where('u.status = :status')
        ->andWhere('u.type = :type')
        ->andWhere('u.identity = :identity')
        ->setParameter('status' , 1)
        ->setParameter('type' , 1)
        ->setParameter('identity' , 1);

    $transaction_count = $qb
        ->getQuery()
        ->getSingleScalarResult();