mysql-查询月份名称

qgelzfjb  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(316)

我在symfony中编写了一个查询,返回一年中每个月的总金额。但是当我试着把它从月号转换成月名的时候,它就错了
错误:应为条令\orm\query\lexer::t\u右括号,got“,”
我的密码。。

$resultYearly = $this->getTransactionRepository()
        ->createQueryBuilder('p')
       ->select('MONTH(p.transactionDate, \'%b\') AS MonthOfYear, sum(p.amount) Total')
MonthAndYear, sum(p.amount) Total')
            ->where('p.transactionDate >= :end')
            ->setParameter('end', new \DateTime('-1 year'))
            ->groupBy( 'MonthOfYear')
            ->getQuery()
            ->getArrayResult();

它与date\u格式完美结合,但当我使用month时,它会抛出一个错误。。

gdx19jrr

gdx19jrr1#

你需要移除 \'%b\'Month 功能,因为 MONTH 函数只接受一个参数,即日期。所以你的代码应该是:

$resultYearly = $this->getTransactionRepository()
        ->createQueryBuilder('p')
       ->select('MONTH(p.transactionDate) AS MonthOfYear, sum(p.amount) Total')
MonthAndYear, sum(p.amount) Total')
            ->where('p.transactionDate >= :end')
            ->setParameter('end', new \DateTime('-1 year'))
            ->groupBy( 'MonthOfYear')
            ->getQuery()
            ->getArrayResult();
k3bvogb1

k3bvogb12#

使用

DATE_FORMAT(p.transactionDate,'%b')  AS MonthOfYear

相关问题