sql >> データベース >  >> RDS >> Mysql

1か月あたりのレコードを取得しますが、その月のレコードがない場合はゼロも取得します

    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
    

    年を特定したい場合は、これを試してください。

    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. WindowsにSQLclをインストールする方法は?

    2. MySql C ++コネクタgetString()は正しく機能しませんが、getIntは完全に機能します

    3. MySQL / SQLite / etcデータベースの違いは?

    4. MySQLi PHP:MySQLiを使用してSQLINSERTクエリが完全に成功したかどうかを確認します