【问题标题】:Doctrine Group by month DQLDoctrine Group 按月 DQL
【发布时间】:2015-09-09 23:19:46
【问题描述】:

我有一个问题可以帮助我。我需要每月为 DQL 查询中的日期执行一次 GROUP BY。我看到可以使用 Doctrine Extension 完成,但我需要 MySQL 和 Postgres。这个想法是计算给定参数在一个月内重复的次数。我修复了目前每月执行一个查询,但这不是最佳的,我需要执行一个返回和分组的查询。 提前致谢和问候

【问题讨论】:

  • 你不能使用 GROUP By 吗?

标签: postgresql symfony doctrine-orm group-by dql


【解决方案1】:

不,问题出在 MONTH,DQL 咨询将以这种方式进行: SELECT count(s) FROM TestBundle:Entity s GROUP BY MONTH(s.date) MONTH 函数在 Postgres 的 DQL 中不存在,另一种选择?

【讨论】:

    【解决方案2】:

    我不知道 DQL 是什么意思,但是在 PostgreSQL 中你可以按以下方式按月分组:

    select
      extract (year from your_date) || '-' ||
      extract (month from your_date), count (*)
    from your_table
    where your_parameter = <whatever>
    group by
      extract (year from your_date), extract (month from your_date)
    

    或者我个人最喜欢的:

    select date_trunc ('month', your_date)::date, count (*)
    from your_table
    where your_parameter = <whatever>
    group by date_trunc ('month', your_date)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多