doumie7914 2017-12-31 23:38
浏览 111
已采纳

学说 - 按日期分组

I have this query :

SELECT DATE_FORMAT(exp_date, "%Y-%m") AS Month, sum(exp_total) FROM export
GROUP BY DATE_FORMAT(exp_date, "%Y-%m");

I tried to convert it to Symfony doctrine like below :

$qb = $this
  ->createQueryBuilder('e')
  ->select('DATE_FORMAT(e.expDate, \'%Y-%m\'), sum(e.expTotal) total')
  ->groupBy('DATE_FORMAT(e.expDate, \'%Y-%m\')');
return $qb->getQuery()->getResult();

Using:

"beberlei/DoctrineExtensions": "^1.0"

Error: [Semantical Error] line 0, col 103 near 'DATE_FORMAT(e.expDate,': Error: Cannot group by undefined identification or result variable.

  • 写回答

1条回答 默认 最新

  • dongzhan1878 2018-01-01 07:06
    关注

    I assume you have configured the mentioned bundle and added the required configuration like

    doctrine:
        orm:
            dql:
                datetime_functions:
                    date_format: DoctrineExtensions\Query\Mysql\DateFormat
    

    Now in query you can assign an alias as dateAsMonth to the result of DATE_FORMAT expression and in group by you can use this alias

    return $qb =   $this->createQueryBuilder('e')
                        ->select('DATE_FORMAT(e.expDate, \'%Y-%m\') as dateAsMonth, sum(e.expTotal) total')
                        ->groupBy('dateAsMonth')
                        ->getQuery()
                        ->getResult();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看