如果表格具有这种结构,有没有办法获得几个月的列表:
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
我希望获得一个列表,其中月份唯一,如下所示:
Months
2013-10-01
2013-11-01
2014-01-01
2014-02-01
2014-03-01
2014-04-01
可行吗?或者我是否需要进行其他类型的查询并在PHP中使用它?
答案 0 :(得分:7)
SELECT DATE_FORMAT(date, '%Y-%m-01') as Months from Table GROUP BY Months;
此:
答案 1 :(得分:2)
试试这个:
select DATE_FORMAT(date, '%Y-%m-01') as Months from Table group by Months
基于这个问题:MySQL round date to the start of the week and month
和此文档:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
答案 2 :(得分:1)
我认为你想要的是:
SELECT date FROM tableName GROUP BY date;
答案 3 :(得分:1)
这将按日期的每个月对日期进行分组
SELECT
DATE(date) as your_date
FROM myTable
GROUP BY MONTH(your_date);
处理多年,你也可以按年分组..
GROUP BY MONTH(your_date), YEAR(your_date)
答案 4 :(得分:0)
SELECT DISTINCT DATE_FORMAT(date, '%Y-%m-01') FROM Table