2

这是我的查询:

$chart_result = mysql_query("SELECT * 
                             FROM (SELECT DATE(timestamp) AS ts, 
                                          YEAR(timestamp) AS year, 
                                          MONTH(timestamp) AS month, 
                                          DAY(timestamp) AS day, 
                                          SUM(paymentgross) AS sales 
                                   FROM stats_ans_1 
                                   GROUP BY DATE(timestamp) DESC 
                                   LIMIT 10
                                  ) AS TBL 
                             GROUP BY ts");

这是时间戳:

2012-09-12 19:45:23

MONTH(timestamp) 绝对确定时间戳是在 10 月而不是 9 月 - 这显然是错误的。怜悯我可怜的脑细胞!

4

1 回答 1

2

您可以使用DATE_FORMAT函数以字符串格式返回月份:

SELECT DATE_FORMAT('2012-09-12 19:45:23', '%M') AS `month`;

你也不需要使用GROUP BY两次,试试这个查询:

SELECT DATE(timestamp) AS ts,
       YEAR(timestamp) AS year,
       MONTH(timestamp) AS month,
       DAY(timestamp) AS day,
       SUM(paymentgross) AS sales
FROM stats_ans_1
GROUP BY DATE(timestamp) DESC
LIMIT 10;
于 2012-09-13T13:06:27.707 回答