9

How to write a following SQL query in doctrine2 as DQL .

SELECT COUNT(id)  
FROM stats  
WHERE YEAR(record_date) = 2009  
GROUP BY YEAR(record_date), MONTH(record_date)

i.e i would like to group by results based on month,year of datetime field stored in MySQL table.

VisioN
  • 143,310
  • 32
  • 282
  • 281
afsar
  • 111
  • 1
  • 2
  • 4

1 Answers1

11

In DQL you could also group by month, year, day etc with SUBSTRING.

For example - group by month (datetime format Y-m-d H:i:s):

SELECT p, SUBSTRING(p.date, 6, 2) as month
FROM Entity p
GROUP BY month
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
repincln
  • 2,029
  • 5
  • 24
  • 34