DQL Select every rows having one column's MAX value

后端 未结 3 1945
囚心锁ツ
囚心锁ツ 2021-01-03 05:28

Working with Symfony 2 and Doctrine, I\'m searching for a way to select every rows having the max value in a specific column.

Right now, I\'m doing it in two queries

3条回答
  •  萌比男神i
    2021-01-03 06:11

    After some hours of headache and googling and stackOverflow readings... I finally found out how to make it.

    Here is my final DQL queryBuilder code:

        $qb = $this->createQueryBuilder('a');
        $qb2= $this->createQueryBuilder('mss')
                ->select('MAX(mss.periodeComptable) maxPeriode')
                ->where('mss.affaire = a')
                ;
    
        $qb ->innerJoin('GAAffairesBundle:MontantMarche', 'm', 'WITH', $qb->expr()->eq( 'm.periodeComptable', '('.$qb2->getDQL().')' ))
            ->where('a = :affaire')
            ->setParameter('affaire', $affaire)
            ;
    
        return $qb->getQuery()->getResult();
    

提交回复
热议问题