Tengo 4 tablas y Wallpapers, Downloads, Favorites, Votes que están definidas así. De wallpapers solo necesito el id.
Tabla Downloads
+----+---------+---------+
| id | wall_id | user_id |
+----+---------+---------+
| 1 | 1 | 1 |
| 2 | 35 | NULL |
| 3 | 35 | NULL |
+----+---------+---------+
Tablas Favorites
+----+---------+---------+
| id | user_id | wall_id |
+----+---------+---------+
| 1 | 12 | 10 |
| 2 | 12 | 2 |
+----+---------+---------+
Tabla Votes
+----+---------+---------+---------+
| id | user_id | wall_id | type |
+----+---------+---------+---------+
| 1 | 12 | 1 | dislike |
| 2 | 12 | 39 | like |
| 3 | 1 | 2 | like |
| 4 | 2 | 2 | like |
| 5 | 3 | 2 | like |
| 6 | 5 | 2 | dislike |
| 7 | 12 | 10 | like |
| 8 | 12 | 2 | like |
+----+---------+---------+---------+
Básicamente lo que necesito es una consulta que me devuelva la cantidad de Descargas, Favoritos, Likes y Dislikes de un wallpaper. Trate anidando con varios LEFT OUTER JOIN asi:
SELECT w.id,COUNT(d.id) AS Downloads,
COUNT(f.id) AS Favorites,
SUM(IF(v.type = 'like',1,0)) AS Likes,
SUM(IF(v.type = 'dislike',1,0)) AS Dislikes
FROM wallpapers AS w
LEFT OUTER JOIN downloads AS d ON w.id = d.wall_id
LEFT OUTER JOIN favorites AS f ON w.id = f.wall_id
LEFT OUTER JOIN votes AS v ON w.id = v.wall_id
WHERE w.id = 2
GROUP BY w.id;
Que me devuelve el resultado de la forma que necesito pero con los cálculos erróneos es decir se duplican valores y pasan otras cosas extrañas.
Tabla Resultados
+----+-----------+-----------+-------+----------+
| id | Downloads | Favorites | Likes | Dislikes |
+----+-----------+-----------+-------+----------+
| 2 | 10 | 10 | 8 | 2 |
+----+-----------+-----------+-------+----------+
Tengo entendido que es por que los LEFT OUTER JOIN no se pueden anidar así ya que las 3 tablas deben tener registros que correspondan con el id de la primera tabla.
¿Alguna Solucion?