Subquery in the SELECT clause with QueryBuilder ? #5663

Closed
opened 2026-01-22 15:14:05 +01:00 by admin · 1 comment
Owner

Originally created by @sebastiaanviaene on GitHub (Sep 1, 2017).

Originally assigned to: @Ocramius on GitHub.

My question is simple , is it possible to do a subquery in the SELECT with QueryBuilder. I've tried looking this up but have gotten no success yet. Here's the code I have at the moment. :

` $qb = $this->getEntityManager()->createQueryBuilder();
$qb ->select('count(t.id)')
->from('AppBundle:Talent','t')
->where('t.status = :active')
->setParameter('active', 'ACTIVE');

    $qb2 = $this->getEntityManager()->createQueryBuilder();
    $qb2 ->select('count(v.id)')
        ->from('AppBundle:Vacancy','v')
        ->where('v.status = :active')
        ->setParameter('active', 'ACTIVE');
        

    $query = $this->getEntityManager()->createQueryBuilder()
        ->select('d')
        ->addSelect($qb->getDQL())
        ->addSelect($qb2->getDQL())
        ->from('dual', 'd');

    $query->getQuery()->getResult();`

But I get the error :

"[Syntax Error] line 0, col 10: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'SELECT'"

Originally created by @sebastiaanviaene on GitHub (Sep 1, 2017). Originally assigned to: @Ocramius on GitHub. My question is simple , is it possible to do a subquery in the SELECT with QueryBuilder. I've tried looking this up but have gotten no success yet. Here's the code I have at the moment. : ` $qb = $this->getEntityManager()->createQueryBuilder(); $qb ->select('count(t.id)') ->from('AppBundle:Talent','t') ->where('t.status = :active') ->setParameter('active', 'ACTIVE'); $qb2 = $this->getEntityManager()->createQueryBuilder(); $qb2 ->select('count(v.id)') ->from('AppBundle:Vacancy','v') ->where('v.status = :active') ->setParameter('active', 'ACTIVE'); $query = $this->getEntityManager()->createQueryBuilder() ->select('d') ->addSelect($qb->getDQL()) ->addSelect($qb2->getDQL()) ->from('dual', 'd'); $query->getQuery()->getResult();` But I get the error : "[Syntax Error] line 0, col 10: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | \"(\" Subselect \")\" | CaseExpression, got 'SELECT'"
admin added the Can't FixQuestion labels 2026-01-22 15:14:05 +01:00
admin closed this issue 2026-01-22 15:14:05 +01:00
Author
Owner

@Ocramius commented on GitHub (Sep 1, 2017):

This is not supported as per the EBNF

@Ocramius commented on GitHub (Sep 1, 2017): This is not supported as per the [EBNF](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#ebnf)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5663