2017-04-25 51 views
1

我有一個日誌表,我想顯示在圖表中的細節,所以我想要一個查詢,將顯示所有記錄從一月到當月,如果當月的活動是零,它應該返回爲零,使得圖表可以流動,從mysql計數返回一個零即使它沒有結果該月

可能表看起來像這樣:

id  user_id action     date_added 
75  56  Base Adding   2017-02-29 11:52:35 
76  56  Base Adding   2017-02-29 11:59:28 
77  61  Fuel Adding   2017-02-29 12:05:16 
78  56  Update details   2017-02-29 12:17:08 
79  57  Logged In    2017-02-29 12:25:41 
80  56  Base Adding   2017-03-29 12:40:56 
81  55  Logged In    2017-02-29 12:51:31 
82  56  Base Adding   2017-05-29 12:53:25 

我想要的頂部看到的結果是一樣的東西這個:

MonthAdded   Totalcount 
2017-01    0 
2017-02    2 
2017-03    1 
2017-04    0 
2017-05    1 

我的查詢看起來像這樣,它不給我輸出,請幫助我。

SELECT DATE_FORMAT(date_added, '%Y-%m') AS MonthAdded, COUNT(id) AS Totalcost FROM audit_log WHERE date_added LIKE '%2017%' AND user_id = '56' AND (action = 'Base Adding') GROUP BY DATE_FORMAT(date_added, '%Y-%m') ASC 
+0

這是不可能的..你有這樣的2017-02超過2個字。 –

+0

針對基本添加的操作對於2017年爲2 – Rhynix

+0

明確提出問題... –

回答

0

這裏的細節根據您的樣本數據:

詳細結果:

select MonthAdded,count(date_substr) as cnt from   
(select '2017-01'as MonthAdded UNION all 
select '2017-02' UNION all 
select '2017-03' UNION all 
select '2017-04' UNION all 
select '2017-05' UNION all 
select '2017-06' UNION all 
select '2017-07' UNION all 
select '2017-08' UNION all 
select '2017-09' UNION all 
select '2017-10' UNION all 
select '2017-11' UNION all 
select '2017-12') as a 

left join 

(select id, user_id, action, date_added,SUBSTRING(date_added FROM 1 FOR 7) as date_substr from 
(select 75 as id,  56 as user_id,  'Base Adding' action,   '2017-02-29 11:52:35' as date_added UNION all 
select 76,  56,  'Base Adding',   '2017-02-29 11:59:28' UNION all 
select 77,  61,  'Fuel Adding',   '2017-02-29 12:05:16' UNION all 
select 78,  56,  'Update details' ,  '2017-02-29 12:17:08' UNION all 
select 79,  57,  'Logged In' ,   '2017-02-29 12:25:41' UNION all 
select 80,  56,  'Base Adding',   '2017-03-29 12:40:56' UNION all 
select 81,  55,  'Logged In' ,   '2017-02-29 12:51:31' UNION all 
select 82,  56,  'Base Adding',   '2017-05-29 12:53:25') as a 
where action = 'Base Adding' and user_id = 56) as b 

on a.MonthAdded = b.date_substr 
group by MonthAdded 

最終查詢結構:

select MonthAdded,count(date_substr) as cnt from   
(select '2017-01'as MonthAdded UNION all 
select '2017-02' UNION all 
select '2017-03' UNION all 
select '2017-04' UNION all 
select '2017-05' UNION all 
select '2017-06' UNION all 
select '2017-07' UNION all 
select '2017-08' UNION all 
select '2017-09' UNION all 
select '2017-10' UNION all 
select '2017-11' UNION all 
select '2017-12') as a 

left join 

(select id, user_id, action, date_added,SUBSTRING(date_added FROM 1 FOR 7) as date_substr from 
(select * from yourtable) as a 
where action = 'Base Adding' and user_id = 56) as b 

on a.MonthAdded = b.date_substr 
group by MonthAdded 

它包括直到十二月,如果你確保有一個關於它的數據。

結果:

2017-01 0 
2017-02 2 
2017-03 1 
2017-04 0 
2017-05 1 
2017-06 0 
2017-07 0 
2017-08 0 
2017-09 0 
2017-10 0 
2017-11 0 
2017-12 0 
+0

感謝@reds查詢像魔術一樣工作......只是一件事該查詢的這一行做了什麼SUBSTRING(date_added從1 FOR 7) – Rhynix

+0

它是一個函數,它從1到7之間的任意值獲得像這樣的「2017-12」,長度爲7個字符 –

+0

與DATE_FORMAT(date_added,'%Y-%m 「)? – Rhynix

相關問題