1

Doctrine 2 QueryBuilder でこのクエリを実行する方法:

SELECT AVG(x.distance) avg_distance FROM (SELECT r.* FROM 結果 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.
4

1 に答える 1

1
$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();
于 2013-07-31T09:03:52.210 に答える