sql >> Databasteknik >  >> RDS >> Mysql

MySQL-fråga Välj, SUMMA, LEFT JOIN

Prova detta:

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. Undantag ORA-08103:objektet existerar inte längre vid användning av setfetchsize av Hibernate

  2. visa tabeller med liknande använda Java och MySQL?

  3. Hur hämtar man data från en SQL Server-databas i C#?

  4. Kan jag styra GROUP BY i django 1.3:s orm?