0

I have three tables (MySQL)

forum: each line in this table is a comment in the forum related to the match by static_id and related to the author by user_id

|match_static_id| date | time | comments | user_id |

matches: this table contains matches with all its information

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

iddaa : this table contains a code for each match (some matches do not have codes here)

|match_static_id| iddaa_code |

I make a query like following:

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

the query work as I want (I get the match information, iddaa code for the match if there is one, and the author of the comment(last comment) ). The problem is in the "count function" I should get the number of the comments related to the same match bur the query returned (double of each value) for example if I have 5 comments for a match it returns 10 I want to know if all parts of my query is right and any help will be good?

4

1 回答 1

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
于 2013-07-10T09:34:39.573 回答