2017-02-09 80 views
0
SELECT DATE_FORMAT(es.scheduled_datetime, '%X-%V') AS date, 
    COUNT(es.event_schedule_id) AS total, 
    0 as type 
    FROM event_schedule es ,event_schedule_mapping esm,events e 
    WHERE 
    es.event_schedule_id = esm.event_schedule_id and 
    esm.event_id = e.event_id and 
    es.event_status_id in(1,2) and 
    es.scheduled_datetime BETWEEN 
    '2017-01-01' AND '2017-01-31' 
    GROUP BY date 
    ORDER BY date 

這是一個使用此查詢,我能夠顯示記錄,其計數大於零而低於輸出來這樣如何將數據每週明智MySQL中每個月

一月月

更大我的查詢
'2017-01', '2', '0' 
'2017-02', '2', '0' 
'2017-03', '10', '0' 
'2017-04', '2', '0' 
'2017-05', '9', '0' 

但它沒有顯示,如果計數在第二個星期零,而我必須 顯示也:

'2017-01', '2', '0' 
'2017-02', '0', '0' 
'2017-03', '10', '0' 
'2017-04', '2', '0' 
'2017-05', '9', '0' 

請建議我如何顯示記錄如果計數爲零,它應該明智的請建議我

+0

見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very -simple-sql-query – Strawberry

+0

你期望的輸出是什麼 – affaz

+0

你需要創建一個子查詢來生成你過濾的月份的星期,並在你的'event_schedule'表中使用它的外連接。 –

回答

0

如果你想從數據庫每個月每週獲取數據,我已經查詢過,試試這個查詢。

SELECT DISTINCT EXTRACT(WEEK FROM date) as week,tbl_data.date AS date, SUM(count.calories) AS sum,tbl_data.offset AS offset from tbl_data INNER JOIN count ON count.id = tbl_data.item_id where tbl_data.user_id="+user_id+" and tbl_data.date and MONTH(date) = MONTH(CURRENT_DATE()) GROUP BY week 
相關問題