好的,这是我的问题。我试图在这些年来每月获得专辑数量。这是我的查询
SELECT DATE(a.date), COUNT(a.id)
FROM album a
JOIN event e ON e.id = a.event_id
WHERE YEAR(a.date) >= 2001
AND e.client_id = 89
GROUP BY MONTH(a.date)
ORDER BY a.date DESC
返回的结果是
+--------------+-------------+
| DATE(a.date) | COUNT(a.id) |
+--------------+-------------+
| 2011-09-09 | 15 |
| 2011-08-26 | 9 |
| 2010-01-01 | 28 |
| 2006-03-03 | 20 |
| 2005-12-02 | 20 |
| 2005-11-11 | 15 |
| 2005-10-07 | 13 |
| 2005-07-01 | 12 |
| 2005-06-03 | 6 |
| 2005-05-06 | 18 |
| 2005-04-15 | 15 |
| 2005-02-04 | 22 |
+--------------+-------------+
12 rows in set (0.00 sec)
这个结果集缺少 2012 年的结果但是如果我写这个
SELECT DATE(a.date), COUNT(a.id)
FROM album a
JOIN event e ON e.id = a.event_id
WHERE
YEAR(a.date) > 2005
AND e.client_id = 89 GROUP BY MONTH(a.date)
ORDER BY a.date DESC;
现在我得到了 2012 年,但我错过了 2005 年:/ 有人可以向我解释一下吗?
+--------------+-------------+
| DATE(a.date) | COUNT(a.id) |
+--------------+-------------+
| 2012-06-01 | 2 |
| 2012-05-04 | 14 |
| 2012-04-06 | 13 |
| 2011-12-02 | 10 |
| 2011-11-04 | 9 |
| 2011-10-07 | 10 |
| 2011-09-09 | 9 |
| 2011-08-26 | 6 |
| 2010-01-01 | 26 |
| 2006-07-07 | 8 |
| 2006-03-03 | 19 |
| 2006-02-03 | 16 |
+--------------+-------------+
12 rows in set (0.01 sec)