我有一個奇怪的行爲,如果有人能解釋這一點,我會很高興嗎?今天是2016年5月22日(面向未來的讀者)。MySQL:BETWEEN - 查詢中的不同結果
SELECT `Opened`,
`Milestone_Status`,
Year(`Opened`),
Month(`Opened`),
Count(*) As Total_Rows
FROM Cases
WHERE `Opened` BETWEEN (CURRENT_DATE() - INTERVAL 6 MONTH) AND (CURRENT_DATE() - INTERVAL 2 MONTH)
GROUP BY Year(`Opened`), Month(`Opened`), `Milestone_Status`;
結果如下:
+------------+------------------+----------------+-----------------+------------+
| Opened | Milestone_Status | Year(`Opened`) | Month(`Opened`) | Total_Rows |
+------------+------------------+----------------+-----------------+------------+
| 2016-12-07 | Compliant | 2016 | 12 | 2 |
| 2017-01-10 | Compliant | 2017 | 1 | 6 |
| 2017-02-23 | Closed Violation | 2017 | 2 | 1 |
| 2017-02-27 | Compliant | 2017 | 2 | 1 |
| 2017-03-01 | Closed Violation | 2017 | 3 | 1 |
| 2017-03-06 | Compliant | 2017 | 3 | 2 |
+------------+------------------+----------------+-----------------+------------+
6 rows in set (0.02 sec)
所以,當我改變從2一個月的差異爲3,它應該顯示的線條,直到本月( '開')2,但..
SELECT `Opened`,
`Milestone_Status`,
Year(`Opened`),
Month(`Opened`),
Count(*) As Total_Rows
FROM Cases WHERE `Opened` BETWEEN (CURRENT_DATE() - INTERVAL 6 MONTH) AND (CURRENT_DATE() - INTERVAL 3 MONTH)
GROUP BY Year(`Opened`), Month(`Opened`), `Milestone_Status`;
result:
+------------+------------------+----------------+-----------------+------------+
| Opened | Milestone_Status | Year(`Opened`) | Month(`Opened`) | Total_Rows |
+------------+------------------+----------------+-----------------+------------+
| 2016-12-07 | Compliant | 2016 | 12 | 2 |
| 2017-01-10 | Compliant | 2017 | 1 | 6 |
+------------+------------------+----------------+-----------------+------------+
2 rows in set (0.01 sec)
任何想法,更好的方法,請嗎?
well * 2017 ???? –
CURRENT_DATE() - INTERVAL 6 MONTH == 2016-11-22和CURRENT_DATE() - INTERVAL 3 MONTH == 2017-02-22,所以一切正常與您的查詢:) – Jovana