如何在DQL中使用MAX(列值)选择行? [英] How can I SELECT rows with MAX(Column value) in DQL?

查看:136
本文介绍了如何在DQL中使用MAX(列值)选择行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的表:

  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();
}

但是显示以下错误:

  [语法错误]行0,col -1:错误:预期Doctrine\ORM\Query\Lexer :: T_CLOSE_PARENTHESIS,结束字符串。 (500内部服务器错误)


解决方案

尝试使用另一个别名子选择为:

  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();
}

希望这个帮助


I can not get this query in a symfony2 project that I have.

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 

I need to get the course whose value in the "numOrden" column is the maximum( in this case it would be the 3rd course). For this I have used the following query in 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();
}

Or

    public function findCourse()
{
    return $this->getEntityManager()->createQuery(
    'SELECT c FROM Bundle:Course c WHERE c.numOrden=(SELECT max(c.numOrden) FROM Bundle:Course )')
    ->getResult();
}

But it shows the following error:

[Syntax Error] line 0, col -1: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got end of string. (500 Internal Server Error)  

解决方案

Try using another alias in the subselect as:

    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();
}

Hope this help

这篇关于如何在DQL中使用MAX(列值)选择行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆