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

Ottieni SUM in GROUP BY con JOIN usando MySQL

Usa:

  SELECT p.department,
         SUM(p.price) AS total_price
    FROM PRODUCTS p
    JOIN (SELECT DISTINCT 
                 r.product_id,
                 r.rating
            FROM RATINGS r) x ON x.product_id = p.id
                             AND x.rating = 5
GROUP BY p.department

Tecnicamente, questo non utilizza una sottoquery:utilizza una tabella derivata/vista in linea.