2013-08-23 46 views
9

我正在編寫一個查詢,以提供按日期排序的過去一年中提交的報表數量。 我獲取當前年份和月份用PHP:即使沒有相應的mysql結果,如何獲得計數?

$year = date('Y') - 1; 
$month = date('m'); 

,並執行以下查詢: SQL:

SELECT month(date_lm) AS `month` , 
count(*) AS `count` 
FROM `reports` 
WHERE (status = 'submitted') 
AND (date_lm > 2012-08) 
GROUP BY month(date_lm) 
ORDER BY month(date_lm) ASC 

而且由於只已在去年提交1只給我1個結果...

| month | count | 
| 7 | 1 | 

但我想結果集中顯示:

| month | count | 
| 9 | 0 | 
| 10 | 0 | 
| 11 | 0 | 
| 12 | 0 | 
| 1 | 0 | 
| 2 | 0 | 
| 3 | 0 | 
| 4 | 0 | 
| 5 | 0 | 
| 6 | 0 | 
| 7 | 1 | 
| 8 | 0 | 

這可能嗎?

回答

2

你應該LEFT JOIN此表1..12表。 事情是這樣的:

SELECT Months.id AS `month` , 
COUNT(`reports`.date_lm) AS `count` 
FROM 
(
    SELECT 1 as ID UNION SELECT 2 as ID UNION SELECT 3 as ID UNION SELECT 4 as ID 
    UNION 
    SELECT 5 as ID UNION SELECT 6 as ID UNION SELECT 7 as ID UNION SELECT 8 as ID 
    UNION 
    SELECT 9 as ID UNION SELECT 10 as ID UNION SELECT 11 as ID UNION SELECT 12 as ID 
) as Months 
LEFT JOIN `reports` on Months.id=month(`reports`.date_lm) 
         AND 
         (status = 'submitted') 
         AND (date_lm > 2012-08) 
GROUP BY Months.id 
ORDER BY Months.id ASC 

SQL Fiddle demo

+0

嗨,謝謝,這似乎是合法的,但我得到一個FUNCTION報告。月不存在。請使用函數名稱解析和解析進行檢查。 – surfer190

+0

@StevieG:嘗試固定查詢 – valex

+0

它現在正在工作,但輸出仍然不會輸出從9到8個月的數據。所以只有當滿足條件的行被添加時。所以沒有0計數。 – surfer190

3

爲了做到這一點,您可以創建一個「月」表,然後在該表和報表中使用左外連接。

我從未使用過的MySQL所以道歉,如果語法稍微偏離,但是這將是查詢:

SELECT months.monthNumber, 
    count(reports.id) AS `count` 
FROM `months` left outer join `reports` on months.monthNumber = month(reports.date_lm) 
WHERE (status = 'submitted') 
AND (date_lm > 2012-08) 
GROUP BY monthNumber 
ORDER BY monthNumber ASC 

重要的是,數應在報告表中的列,而不是幾個月的表,否則你永遠不會得到零。

0

希望這可以幫助..

SELECT t1.month_year AS month, COALESCE(SUM(t1.total+t2.total), 0) AS count FROM ( SELECT DATE_FORMAT(a.Date, "%Y-%m") AS md, DATE_FORMAT(a.Date, "%b-%y") AS month_year, '0' AS total FROM ( SELECT curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c ) a WHERE a.Date <= NOW() AND a.Date >= Date_add(Now(),INTERVAL - 11 MONTH) GROUP BY md )t1 LEFT JOIN ( SELECT DATE_FORMAT(created_at, "%b") AS month, COUNT(*) AS total ,DATE_FORMAT(created_at, "%Y-%m") AS md FROM reports WHERE created_at <= NOW() AND created_at >= Date_add(Now(),INTERVAL - 11 MONTH) AND狀態= 1 GROUP BY md )t2 ON t2.md = t1.md LEFT JOIN ( SELECT DATE_FORMAT(date_lm, "%b") AS month, COUNT(*) AS total ,DATE_FORMAT(date_lm, "%Y-%m") AS md FROM tbl_users WHERE date_lm <= NOW() AND date_lm >= Date_add(Now(),INTERVAL - 11 MONTH) AND status = 'submitted' GROUP BY md )t3 ON t3.md = t1.md GROUP BY t1.md ORDER BY t1.md

這會給爲過去12個月的數據即使它沒有數據一個0 ..

相關問題