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

MySQL LEFT JOIN con GROUP BY e WHERE IN (sottoquery)

SELECT  a.date, COUNT(b.campaignid) totalStat
FROM    campaigndata a
        LEFT JOIN campaignfilter b
            ON  a.campaignid = b.campaignid AND
                b.campaigntype = 1
GROUP   BY a.date

Per acquisire ulteriori informazioni sui join, visitare gentilmente il collegamento seguente:

AGGIORNAMENTO 1

SELECT  a.date, 
        COALESCE(b.totals,0) totals
FROM    demo_calendar a
        LEFT JOIN
        (
            SELECT  a.date, SUM(impressions) totals
            FROM    demo_campaigndata a
                    INNER JOIN demo_campaignfilter b
                        ON a.campaignid = b.campaignid
            WHERE   b.campaigntype = 1
            GROUP   BY a.date
        ) b ON a.date = b.date