DQL / SQL GROUP BY MONTH()

时间:2017-11-16 10:16:47

标签: php sql symfony dql

GROUP BY月份是否可以只将日期作为列?这可以通过PHP循环实现,但我更喜欢使用SQL。现场代码如下:

    $query = $this->em
            ->createQuery("SELECT Month(p.planDate), SUM(ROUND(p.ndata5/1000)) AS volume "
                    . "FROM NTPBundle:ParagonData p WHERE p.planDate >= :startDate AND p.ndata5<>0 GROUP BY Month(p.planDate) ORDER BY p.planDate")
            ->setParameter('startDate', $this->startDate->format('Y-m-d'));

1 个答案:

答案 0 :(得分:0)

正确排序你必须命名选择然后ORDER BY这个名字如:

 $query = $this->em
            ->createQuery("SELECT Month(p.planDate) as monthPlan, SUM(ROUND(p.ndata5/1000)) AS volume "
                    . "FROM NTPBundle:ParagonData p WHERE p.planDate >= :startDate AND p.ndata5<>0 "
                    . "GROUP BY monthPlan "
                    . "ORDER BY p.planDate")
            ->setParameter('startDate', $this->startDate->format('Y-m-d'));

感谢线索