0
我已經建立了下面的查詢嘗試&做到以下幾點:MySQL查詢案例當30天,60天,而不是預定輸出
- 如果omSchedDate = 0〜30天今天則標誌「30天」
- 如果omSchedDate = 31〜60天的今天則標誌‘60天’
如果omSchedDate前一天0天或60天,然後後旗屬於‘未調度’
SELECT DATEDIFF(tblcom.omSchedDate, CURDATE()) AS age, CASE WHEN tblcom.omSchedDate > CURDATE() AND tblcom.omSchedDate < (DATE_ADD(CURDATE(), INTERVAL 31 DAY))THEN '30 Days' WHEN DATEDIFF(CURDATE(), tblcom.omSchedDate) > 30 THEN '60 Days' WHEN DATEDIFF(CURDATE(), tblcom.omSchedDate) <= 0 then 'Not Scheduled' WHEN DATEDIFF(CURDATE(), tblcom.omSchedDate) IS NULL then 'Not Scheduled' ELSE NULL END AS consch FROM tblcom ORDER BY tblcom.omSchedDate
我上面的查詢爲30天列提供了正確的輸出,但「60天」&「未安排」列是不正確的?
Age, conSch
-728,60 Days
-726,60 Days
-715,60 Days
-666,60 Days
-102,60 Days
-88,60 Days
-46,60 Days
-15,
-14,
-5,
-4,
2,30 Days
3,30 Days
6,30 Days
14,30 Days
27,30 Days
28,30 Days
30,30 Days
41,Not Scheduled
41,Not Scheduled
83,Not Scheduled
188,Not Scheduled
dg99,美麗的..你的代碼的伎倆只有一個錯字: - END CASE AS consch 應該是: - END AS consch – jlig
好點。我在混合[CASE語句](https://dev.mysql.com/doc/refman/5.0/en/case.html)和CASE表達式。我修好了。 – dg99
嗨dg99,如果我只需要將負值標記爲「不適用」,該怎麼辦? – jlig