1

Is there a way to get a list of months back if a table has this structure:

Table
id    | payment  | date
1001    200.00     2013-10-11
1001    100.00     2013-11-02
1002    250.00     2014-01-23
1003    320.00     2014-02-02
1004    300.00     2014-03-04
1004     90.00     2014-03-05
1004     50.00     2014-04-21
1005    400.00     2014-04-21

I want to get a list back where the month is unique like so:

Months
2013-10-01
2013-11-01
2014-01-01
2014-02-01
2014-03-01
2014-04-01

Is that doable? Or would I have to have some other kind of query and play with it in PHP?

Burning Hippo
  • 787
  • 1
  • 20
  • 47
  • If you have a Calendar table (essentially, a table with all dates in it, plus a few related attributes split off) this becomes trivial. Do you need any _other_ information from this table, or you just want the list of dates? – Clockwork-Muse Apr 13 '14 at 03:37

5 Answers5

7
SELECT DATE_FORMAT(date, '%Y-%m-01') as Months from Table GROUP BY Months;

This:

  • Selects the date in the format you specified (i.e., as first day of month)
  • Groups by month so that you get one record returned for each month
ff524
  • 387
  • 4
  • 19
2

try this: select DATE_FORMAT(date, '%Y-%m-01') as Months from Table group by Months

based off this question: MySQL round date to the start of the week and month

and this doc: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

Community
  • 1
  • 1
pennstatephil
  • 1,593
  • 3
  • 22
  • 43
1

I think what you want is this:

SELECT date FROM tableName GROUP BY date;

Outsider
  • 550
  • 3
  • 9
1

this will group your dates by month of each date

SELECT 
    DATE(date) as your_date 
FROM myTable 
GROUP BY MONTH(your_date);

to handle multiple years, you can also group by year..

GROUP BY MONTH(your_date), YEAR(your_date)
John Ruddell
  • 25,283
  • 6
  • 57
  • 86
0
SELECT DISTINCT DATE_FORMAT(date, '%Y-%m-01') FROM Table
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82