如何返回2 COUNT()

时间:2011-05-06 19:27:45

标签: mysql sql aggregate-functions

我有这张桌子:

forum_categories

  • ID
  • 标题

forum_topics

  • id
  • category_id(n-1 with forum_categories.id)

forum_messages

  • id
  • topic_id(n-1 with forum_topics.id)
  • 第一

我想返回forum_categories.titlecount(forum_topics.id) (我的意思是该类别的话题数量)count(forum_messages.id) (我的意思是号码)字段第一个为0)的主题消息

这是一个例子:

forum_categories            forum_topics            forum_messages
1   title1                  1   1                   1       1       0
2   title2                  2   2                   2       2       1
3   title3                  3   1                   3       3       1
                            4   1                   4       6       1
                            5   3                   5       7       0
                            6   3                   6       2       0
                            7   2                   7       1       1
                                                    8       3       0
                                                    9       5       0
                                                    10      7       1
                                                    11      5       1
                                                    12      1       0

输出必须是:

title1      3       3 (topic 1=2 + topic 3=1 topic4=0
title2      2       2 (topic2=1 + topic7=1)
title3      2       1  (topic5=1 + topic6=0)

尝试过像:

SELECT forum_categories.description, 
       COUNT(forum_topics.id), 
       COUNT(forum_messages.id)
  FROM forum_categories 
  JOIN forum_topics 
  JOIN forum_messages ON forum_categories.id = forum_topics.category_id 
                     AND forum_topics.id = forum_messages.topic_id
GROUP BY forum_categories.id, forum_messages.id
ORDER BY forum_categories.date

但它绝对远离我的目标(而且我也不知道如何查看* forum_messages.first *字段!有什么帮助吗?

修改

感谢Jim Rubenstein,这是一个部分解决方案:

SELECT fc.description, COUNT(DISTINCT ft.id) topics, COUNT(fm.id) 
FROM forum_categories fc 
JOIN forum_topics ft ON ft.category_id = fc.id 
JOIN forum_messages fm ON fm.topic_id = ft.id
GROUP BY fc.id ORDER BY fc.date 

此查询的问题是从forum_messages中计算每条消息,但我需要仅在 field = 0 时为每个主题计算这些消息。

编辑2

我想我找到了解决方案:

SELECT fc.title, COUNT(DISTINCT ft.id) topics, COUNT(fm.id) 
FROM forum_categories fc 
JOIN forum_topics ft ON ft.category_id = fc.id 
LEFT OUTER JOIN (SELECT id, topic_id, first FROM forum_messages WHERE first=0) fm ON fm.topic_id = ft.id
GROUP BY fc.id ORDER BY fc.date 
你怎么看?我能改善吗?

2 个答案:

答案 0 :(得分:2)

应该能够使用DISTINCT关键字完成此操作。您还希望GROUP BY category_id,以便计算每个类别的主题/消息。

SELECT fc.description, COUNT(DISTINCT ft.id) topics, COUNT(fm.id) messages
FROM forum_categories fc
JOIN forum_topics ft ON ft.category_id = fc.id
JOIN form_messages fm ON fm.topic_id = ft.id
WHERE fm.first = 0
GROUP BY fc.id
ORDER BY fc.date

另一种选择:

SELECT fc.description, COUNT(DISTINCT ft.id) topics, SUM(CASE WHEn fm.first = 0 THEN 1 ELSE 0) messages
FROM forum_categories fc
JOIN forum_topics ft ON ft.category_id = fc.id
JOIN form_messages fm ON fm.topic_id = ft.id
WHERE fm.first = 0
GROUP BY fc.id
ORDER BY fc.date

注意:我把你的表别名为较短的名字......因为我懒得一遍又一遍地输入全名。哈哈。

答案 1 :(得分:0)

您可以使用以下子查询:

 select (select count(*) from ...., select count(*) from ....)