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?
Change the group by
GROUP BY `images`.`id`
You want to group by the table that contains all images.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.