sql >> Database >  >> RDS >> Mysql

Ontvang record per maand, maar krijg ook nul als er die maand geen records zijn

SELECT  Months.m AS month, COUNT(Tests.receiveDate) AS total FROM 
(
    SELECT 1 as m 
    UNION SELECT 2 as m 
    UNION SELECT 3 as m 
    UNION SELECT 4 as m 
    UNION SELECT 5 as m 
    UNION SELECT 6 as m 
    UNION SELECT 7 as m 
    UNION SELECT 8 as m 
    UNION SELECT 9 as m 
    UNION SELECT 10 as m 
    UNION SELECT 11 as m 
    UNION SELECT 12 as m
) as Months
LEFT JOIN Tests  on Months.m = MONTH(Tests.receiveDate)  
GROUP BY
    Months.m

Als je een specifiek jaar wilt, probeer dit dan.

SELECT  Months.m AS month, COUNT(Tests.receiveDate) AS total FROM 
(
    SELECT 1 as m 
    UNION SELECT 2 as m 
    UNION SELECT 3 as m 
    UNION SELECT 4 as m 
    UNION SELECT 5 as m 
    UNION SELECT 6 as m 
    UNION SELECT 7 as m 
    UNION SELECT 8 as m 
    UNION SELECT 9 as m 
    UNION SELECT 10 as m 
    UNION SELECT 11 as m 
    UNION SELECT 12 as m
) as Months
LEFT JOIN Tests on Months.m = MONTH(Tests.receiveDate)  
AND YEAR(Tests.receiveDate) = '2012'
GROUP BY Months.m


  1. MySQL-queryfout met CASE en INNER JOIN

  2. Hoe leeftijd in jaren, maanden en dagen te krijgen met Oracle

  3. php/mysql rijen bij elkaar optellen om het totaal te krijgen

  4. UTF-8 tekencodering gevechten json_encode()