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

MySQL seleziona unisci più tabelle e SUM

puoi lasciare unire ad alcune tabelle derivate nel caso in cui ottieni più voci per data

    SELECT  a.id_master ,
        a.barcode ,
        a.name ,
        a.last_stock ,
        COALESCE(b.`IN`, 0) AS `IN`,
        COALESCE(c.`OUT`, 0) AS `OUT`
FROM    master a
        LEFT JOIN (SELECT barcode, SUM(qty) as `IN` 
                     FROM trans_in 
                     WHERE date(time_trans) = '2016-04-01' 
                     GROUP BY barcode ) b ON a.barcode = b.barcode
        LEFT JOIN (SELECT barcode, SUM(qty) as `OUT`
                     FROM trans_out 
                     WHERE date(time_trans) = '2016-04-01' 
                     GROUP BY barcode ) c  ON a.barcode = c.barcode;