ibynmax
ibynmax

Reputation: 11

Howto Write normal sentence in SQL

I want to write this sentence for DQL :

SELECT id FROM `article` WHERE `created_at` IN (select max(created_at) from article)

I try this one but i have some error :

return $this->createQueryBuilder('p')
            ->select('p.id')
            ->where('p.createdAt IN (select max(p.createdAt)from p)')
            ->getQuery()
            ->getResult();

Error :

[Semantical Error] line 0, col 88 near 'p)': Error: Class 'p' is not defined.

Thanks a lot, ibynmax

Upvotes: 1

Views: 53

Answers (1)

Timo
Timo

Reputation: 69

You'll also need to write out the subquery in the query builder:

$subQuery = $this->createQueryBuilder('p')
    ->select('max(p.createdAt)');

$queryBuilder = $this->createQueryBuilder('p');

return $queryBuilder
    ->select('p.id')
    ->where($queryBuilder->expr()->in('p.createdAt', $subQuery->getDQL()))
    ->getQuery()
    ->getResult();

Upvotes: 1

Related Questions