我想为DQL写下这句话:
SELECT id FROM `article` WHERE `created_at` IN (select max(created_at) from article)
我尝试了这个,但是我有一些错误:
return $this->createQueryBuilder('p')
->select('p.id')
->where('p.createdAt IN (select max(p.createdAt)from p)')
->getQuery()
->getResult();
错误:
[Semantical Error] line 0, col 88 near 'p)': Error: Class 'p' is not defined.
非常感谢, ibynmax
答案 0 :(得分:1)
您还需要在查询构建器中写出子查询:
$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();