我正在尝试将此 SQL 转换为 DQL 或任何查询构建器变体的样子。
select *
from project_release r
where (select s.title as status_name
from release_status_log l
left join release_status s
on l.release_status_id = s.id
where l.release_id = r.id
order by l.created_at desc
limit 1
) not in ('Complete', 'Closed')
;
从Release
实体的存储库类内部,我试过这个
return $this->getEntityManager()->createQuery("
select r.*
from MyBundle:Release r
where (select s.title
from MyBundle:ReleaseStatusLog l
join l.status s
where l.release = r
order by l.createdAt desc
limit 1
) IN ('Complete','Closed')
order by r.release_date ASC
limit 10
")->getArrayResult();
这给出了错误
[语法错误] 第 0 行,第 265 列:错误:预期的 Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS,得到“限制”
这是指limit 1
子查询中的。
所以我尝试了这个
return $this
->createQueryBuilder('r')
->select('r.*')
->where("(select s.title
from MyBundle:ReleaseStatusLog l
join l.status s
where l.release = r
order by l.created_at desc
limit 1
) $inClause ('Complete', 'Closed')
")
->setMaxResults( $limit )
->orderBy('release_date', 'ASC')
->getQuery()
->getArrayResult()
;
这给出了同样的错误。如何在父查询中执行限制为每行 1 行的子查询?
- Symfony 2.0.15
- 教义 2.1.7
- PHP 5.3.3
- MySQL 5.1.52