Reputation: 398
I have two tables: "images" and "images_votes". It's for a rating system, so I need to count both the votes and the sum of the scores
When I make a LEFT JOIN, not all the rows from "images" appear, only those that have votes, and one more.
Here's the query:
SELECT `images`.`id` AS id, COUNT( images_votes.id_image ) AS votes, SUM( images_votes.val ) AS val
FROM (
`images`
)
LEFT JOIN `images_votes` ON `images`.`id` = `images_votes`.`id_image`
GROUP BY `images_votes`.`id_image`
And here's the complete example:
http://www.sqlfiddle.com/#!2/05526/1
I have 5 images, so I'd expect that the result would return 5 rows. I only get 3: 2 that have votes, and one more.
Why does this happen?
How can I get the 5 (all) rows?
Upvotes: 1
Views: 802
Reputation: 204756
Change the group by
GROUP BY `images`.`id`
You want to group by the table that contains all images.
Upvotes: 3