我想使用理论2查询构建程序执行 SQL :
SELECT c.*, COUNT(s.id) AS studentCount
FROM classes c
LEFT JOIN (
SELECT *
FROM student_classes
WHERE YEAR = 2012
) sc ON c.id = sc.class_id
LEFT JOIN students s ON sc.student_id = s.id
GROUP BY c.id
我试了这个,但没用
$qb = $this->getEntityManager()
->getRepository( Classes )
->createQueryBuilder( c );
$qb->select( c.id AS id, c.name AS name, COUNT(s) AS studentCount );
$qb->leftJoin(
$qb->select( sc1 )
->from( StudentClasses , sc1 )
->where( sc1.year = :year )
->setParameter( year , $inputYear),
sc2
);
$qb->leftJoin( sc2.students , s );
$qb->groupBy( c.id );
return $qb->getQuery()->getScalarResult();
还是我应该使用本地SQL 来代替?
any help would be appreciated, thanks.