×
Inicio Aleatorio

createquerybuilder groupby having count

Sección: Symfony

Creado: 24-11-24

Conditions for aggregate functions such as sum and count should be put in a having clause.

$qb = $this->createQueryBuilder('s');
$qb->select("s");
$qb->join('s.votes', 'v');
$qb->addSelect("COUNT(v.id) as voteCount");
$qb->orderBy('voteCount', "DESC");
$qb->setMaxResults(3);
$qb->setFirstResult(0);
$qb->groupBy('s.id');
$qb->having("count(v.id) < ?1"); //changed from where
$qb->setParameter(1, 5);
$query = $qb->getQuery();
$result = $query->getResult();

Siguiente Publicación