[英]How to use count correctly in sql?
我有兩個表“ matches”和“ forum”,我需要從matches表中獲取匹配信息,該表在forums表中有注釋,因此我使用以下查詢:
SELECT distinct forum.match_static_id, matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_id
WHERE forum.comments_yes_or_no = 1
如果論壇表中有多個評論,我會使用distinct來避免兩次相同的比賽。
問題是我想使用相同的查詢獲取每個匹配評論的數量嗎? 我用 :
SELECT distinct forum.match_static_id, count(forum.comments), matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_i
WHERE forum.comments_yes_or_no = 1
但它只給我一個記錄(這是錯誤的)。 問題是什么 ?? 我需要使用分組依據嗎? 如果是的話,但是在這個擁擠的查詢中呢?
請嘗試以下方法:
SELECT forum.match_static_id, count(matches.id), matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_i
WHERE forum.comments_yes_or_no = 1
GROUP BY forum.id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.