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

MySQL-forespørgsel Vælg, SUM, LEFT JOIN

Prøv dette:

SELECT S.Items as Products, 
       COALESCE(PS.Quantity, 0) as [Products Sold], 
       COALESCE(PP.Quantity, 0) as [Products purchased]
From Stock S 
LEFT JOIN (SELECT Items, SUM(Quantity) AS Quantity 
           FROM products_purchased 
           GROUP BY Items
         ) AS PP ON S.Items = PP.Items 
LEFT JOIN (SELECT Items, SUM(Quantity) AS Quantity 
           FROM products_sold 
           GROUP BY Items
         ) AS PS ON S.Items = PS.Items; 



  1. SQL Server 2017 Management Tools

  2. Sådan stopper du mysqld

  3. MYSQL:COUNT med GROUP BY, LEFT JOIN og WHERE-sætning returnerer ikke nulværdier

  4. Hvordan fikser denne hjemmeside kodningen?