我无法在我拥有的symfony2项目中获得此查询。
My Table:
id course datetime numOrden ---|-----|------------|-------- 1 | 1º | 04/11/2016 | 1 2 | 2º | 04/11/2016 | 2 5 | 3º | 04/11/2016 | 5 3 | 4º | 03/11/2016 | 4 4 | 5º | 03/11/2016 | 3
我需要获取“ numOrden”列中的值最大的课程(在这种情况下,这将是第三课程)。为此,我在Doctrine2中使用了以下查询:
public function findCourse() { return $this->getEntityManager()->createQuery( 'SELECT c FROM BackendBundle:Curso c WHERE c.numOrden in (SELECT max(c.numOrden) FROM BackendBundle:Curso )') ->getResult(); }
或者
public function findCourse() { return $this->getEntityManager()->createQuery( 'SELECT c FROM Bundle:Course c WHERE c.numOrden=(SELECT max(c.numOrden) FROM Bundle:Course )') ->getResult(); }
但它显示以下错误:
[Syntax Error] line 0, col -1: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got end of string. (500 Internal Server Error)
尝试在subselect中使用另一个别名,如下所示:
public function findCourse() { return $this->getEntityManager()->createQuery( 'SELECT c FROM Bundle:Course c WHERE c.numOrden=(SELECT max(co.numOrden) FROM Bundle:Course co )') ->getResult(); }
希望这个帮助