sql >> Database teknologi >  >> RDS >> Mysql

Få rekord pr. måned, men få også nul, hvis ingen rekorder den måned

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

Hvis du vil have et specifikt år, så prøv dette.

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. Kan ikke oprette en tabel i MySQL, fordi den allerede eksisterer

  2. SQL Server Ydeevne TOP CPU Query -2

  3. Hvordan gør man en kolonne unik i SQL?

  4. Kan Django ORM lave en ORDER BY på en bestemt værdi af en kolonne?