The following query returns strange results for me:
SELECT `Statistics`.`StatisticID`, COUNT(`Votes`.`StatisticID`) AS `Score`, COUNT(`Views`.`StatisticID`) AS `Views`, COUNT(`Comments`.`StatisticID`) AS `Comments` FROM `Statistics` LEFT JOIN `Votes` ON `Votes`.`StatisticID` = `Statistics`.`StatisticID` LEFT JOIN `Views` ON `Views`.`StatisticID` = `Statistics`.`StatisticID` LEFT JOIN `Comments` ON `Comments`.`StatisticID` = `Statistics`.`StatisticID` GROUP BY `Statistics`.`StatisticID` LIMIT 0, 10
I’m querying this on a table structure like the following:
(only data relevant to Statistics.StatisticID = 8)
Votes
StatisticID 8
Views
StatisticID 8 8
Comments
StatisticID 8 8 8 8 8
Now if I run this query I get the following result set:
StatisticID Score Views Comments 8 5 5 5
I knwo where the 5 comes from – the number of Comments – and this works if I take the comments statement out. Can anyone debug this as this is out of my reach (I’m relatively new with SQL).
Thanks, Ross
Assuming you have an id field or similar on the votes/views/comments:
Not tested it, but think it should work. (We have to use the different field because the statisticID will always be the same within a given group …)