我有一个列日期的表;它包含几个月的记录,但不是连续的。为了查看存在哪些月份(当前意味着特定月份中有 1 条或多条记录),我尝试进行概述。它应该是这样的:
2013 Jan Feb Mar - May Jun // etc
2012 Jan - - Apr May - // etc
我可以得到当前月份的所有第一天,但被困在如何转置上。如果我这样做:
SELECT
IF (d = 1, month ,'-') AS Jan,
IF (d = 2, month ,'-') AS Feb,
IF (d = 3, month ,'-') AS Mar
FROM
(SELECT
MONTH(date) AS d,
DATE_FORMAT(date, '%Y-%m-01') AS month
FROM
table
GROUP BY
month
ORDER BY
month) m
GROUP BY YEAR(month)
如果给出这个结果:
Jan Feb Mar
- - Mar
- Feb -
虽然它应该给出:
Jan Feb Mar
- - Mar
Jan Feb Mar