获取chart.js的每个月的SUM数据,间隔一年

时间:2018-04-25 21:43:04

标签: mysql

我有一张包含IP会计数据的表,

我希望每个月(jan,feb,mar ...)获得SUM(bytes),从当天开始,为期一年

column: bytes <-- bytes

column: timeanddate <-- timestamp

我的查询:

SELECT (SELECT SUM(bytes) FROM ip_accounting);

请帮忙。

1 个答案:

答案 0 :(得分:0)

我已经找到了想要的答案

SELECT 
    SUM(download_bytes) as download_bytes, 
    SUM(upload_bytes) as upload_bytes, 
    SUM(download_bytes + upload_bytes) as total_bytes, 
    DAY(date) as day, 
    MONTHNAME(date) as month 
FROM 
    traffic_counters 
LEFT JOIN services ON traffic_counters.service_id = services.id 
WHERE 
    date >= (NOW() - INTERVAL 1 MONTH)
GROUP BY 
    MONTH(date),
    DAY(date)
ORDER BY 
    MONTH(date), 
    DAY(date)