次のクエリは、奇妙な結果を返します。
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
次のようなテーブル構造でこれをクエリしています。
(に関連するデータのみStatistics.StatisticID = 8)
投票
StatisticID
8
ビュー
StatisticID
8
8
コメントコメント
StatisticID
8
8
8
8
8
このクエリを実行すると、次の結果セットが得られます。
StatisticID Score Views Comments
8 5 5 5
私は5がどこから来ているのか(コメントの数)を知っています。これは、コメントステートメントを削除すると機能します。これは私の手の届かないところにあるので、誰かがこれをデバッグできますか(私はSQLに比較的慣れていません)。
ありがとう、ロス