2013-07-10 28 views
0

我有三個表(MySQL的)計數不給正確的結果有三個加入

論壇:此表中的每一行是有關比賽用static_id以及與作者的論壇評論由USER_ID

|match_static_id| date | time | comments | user_id | 

比賽:此表中包含的所有信息相匹配

| static_id | localteam_name | visitorteam_name | date | time |....... 

iddaa:此表包含每個匹配代碼(有些比賽沒有在這裏代碼)

|match_static_id| iddaa_code | 

我做一個查詢類似以下內容:

SELECT forum.match_static_id, forum.date, forum.time, 
count(forum.comments) 'comments_no', matches.*, users.username, iddaa.iddaa_code 
FROM forum 
INNER JOIN matches ON forum.match_static_id = matches.static_id 
INNER JOIN users on forum.user_id = users.id 
LEFT JOIN iddaa on forum.match_static_id = iddaa.match_static_id 
GROUP BY forum.match_static_id 
ORDER BY forum.date DESC, forum.time DESC 

因爲我想(我獲得了比賽的信息,如果有一個,而評論的作者的比賽iddaa代碼查詢工作(最新評論))。 問題是在「計數功能」我應該得到相同的匹配相同的匹配評論返回的查詢數(每個值的兩倍) 例如,如果我有5條評論它匹配返回10 我想要要知道我的查詢的所有部分是否正確,並且任何幫助都很好?

+0

不是左連接總是返回相同數量在這個計數?嘗試沒有左連接。 –

+0

我無法刪除左連接,重要的是要獲得每個比賽的iddaa值。在iddaa查詢中的問題,我知道,但我需要一種解決方法 – Basel

回答

0

也許它可以被包裝在一個子查詢?它很難當我沒有表def +數據。

SELECT Sub.*, COUNT(1) 'comments_no' 
FROM 
(
SELECT forum.match_static_id, forum.date, forum.time, 
matches.*, users.username, iddaa.iddaa_code 
FROM forum 
INNER JOIN matches ON forum.match_static_id = matches.static_id 
INNER JOIN users on forum.user_id = users.id 
GROUP BY forum.match_static_id 
) Sub 
LEFT JOIN iddaa on Sub.match_static_id = iddaa.match_static_id 
ORDER BY forum.date DESC, forum.time DESC 
+0

您可能必須寫Count(iddaa.Comments)才能獲得計數 –