简体   繁体   English

按日和月分组原则

[英]Group By day and month Doctrine

I'd like to list my users by birthday, so month and day but not year.我想按生日列出我的用户,所以是月和日而不是年。

I have this query我有这个查询

SELECT * 
FROM user 
WHERE birthDate IS NOT NULL 
GROUP BY MONTH(birthDate), DAY(birthDate)

But I don't know how to use it with Symfony and Doctrine.但我不知道如何将它与 Symfony 和 Doctrine 一起使用。 I tried我试过

$result = $em->getRepository("AcmeApplicationBundle:SecurityServiceUser")
            ->createQueryBuilder('user')
            ->where('user.birthDate IS NOT NULL')
            ->groupBy('MONTH(user.birthDate), DAY(user.birthDate)')
            ->getQuery()
            ->getResult(); 

And

$result = $em->getRepository("AcmeApplicationBundle:SecurityServiceUser")
            ->createQueryBuilder('user')
            ->where('user.birthDate IS NOT NULL')
            ->groupBy('MONTH(user.birthDate)')
            ->groupBy('DAY(user.birthDate)')
            ->getQuery()
            ->getResult(); 

But in both cases I have an error但在这两种情况下我都有一个错误

[Semantical Error] line 0, col 165 near 'MONTH(birthDate),': Error: Cannot group by undefined identification or result variable. [语义错误] 'MONTH(birthDate),'附近的第 0 行第 165 行:错误:无法按未定义的标识或结果变量分组。

You haven't set an alias for your values. 您尚未为值设置别名。 Here is an updated version : 这是一个更新版本:

   $result = $em->getRepository("AcmeApplicationBundle:SecurityServiceUser")
        ->createQueryBuilder('user')
        ->select(' user.username, MONTH(user.birthDate) AS gBmonth, DAY(user.birthDate) AS gBday')
        ->where('user.birthDate IS NOT NULL')
        ->groupBy('gBmonth')
        ->addGroupBy('gBday')
        ->getQuery()
        ->getResult(); 

This should work fine. 这应该工作正常。

You can use one of these:您可以使用以下之一:

format(as.Date,) 

or或者

strftime(source, format)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM