如何在Doctrine 2 QueryBuilder中执行此查询:
SELECT AVG(x.distance)avg_distance FROM(SELECT r。* FROM result r WHERE r.place_id =?GROUP BY r.place_id ORDER BY r.id DESC LIMIT 100
我试试这个:
$dql = $qb
->select('r.*')
->from('CoreBundle:Result', 'r')
->where('r.place = :place')
->orderBy('r.id', 'DESC')
->setMaxResults(100)
->setParameter('place', $place)
->getDQL()
;
$result = $qb
->select('AVG(x.distance) avg_distance')
->from($dql, 'x')
->getQuery()
->getArrayResult();
但没有工作
SELECT r.* FROM': Error: Class 'SELECT' is not defined.
答案 0 :(得分:0)
$sql = "SELECT AVG(x.distance) avg_distance FROM (SELECT r.* FROM result r WHERE r.place_id = :place_id ORDER BY r.id DESC LIMIT 100) x ";
$stmt = $this->em->getConnection()->prepare($sql);
$stmt->bindValue(':place_id', $place->getId());
$stmt->execute();
return $stmt->fetch();