我有像这样的QueryBuilder
$query = $this->getEntityManager()
->createQueryBuilder()
->select('partial master.{id,name}, partial detail.{id,name}')
->from('AppBundle:Master', 'master')
->leftJoin('master.detail', 'detail')
->orderBy('detail.id','DESC')
->getQuery();
return $query->getArrayResult();
主关系是OneTomany,细节关系很多。我试图获取具有详细信息ID的单一细节更高或更低。哪种QueryBuilder可以实现这一目标?示例我有这样的数据:
1.A, 1.B, 1.C, 2.A, 3.A, 3.C
我想要这样的结果
1.A, 2.A, 3.A
$query = $this->getEntityManager()
->createQueryBuilder()
->select('master.id, master.name, detail.id, detail.name')
->from('AppBundle:Master', 'master')
->leftJoin('master.detail', 'detail')
->groupBy('master.id')
->orderBy('detail.id','DESC')
->getQuery()
;
return $query->getArrayResult();
获取您使用的每个主行的最新细节
SELECT m,a
FROM AppBundleEntityMaster m
LEFT JOIN m.detail a
LEFT JOIN AppBundleEntityDetail b
WITH a.master = b.master
AND a.id < b.id
WHERE b.id IS NULL