2012-11-27 76 views
2

選擇不同的列字段和總金額爲它,我有如下表:在MySQL

topic_id(unique) forum_id forum_views 

1002    1885   5 
1003    1893   2 
1004    1885   3 
1005    1892   6 

我怎樣才能產出這樣的:

forum_id forum_views 
1885   8 
1893   2 
1892   6 
(從上表總論壇觀點獨特forum_id)

任何幫助感謝。

這就是我努力:

從table_name的選擇不同forum_id,SUM(topic_views)

+0

您在forum_id – Ric

+0

添加需要組... GROUP BY forum_id – wxyz

回答

6
select forum_id, 
     sum(forum_views) as forum_views 
from jforum_topics 
group by forum_id 
+0

工作。感謝大家。 – daman

0

你只需要一個GROUP BY指定了哪些行你希望SUM應用於;

select forum_id, sum(topic_views) from jforum_topics 
group by forum_id 
order by forum_id 
0

試試這個:

select forum_id, sum(topic_views) 
from jforum_topics 
group by forum_id 
0
select forum_id, sum(topic_views) from jforum_topics group by forum_id 
1
select forum_id, sum(forum_views) as sum from table_name group by forum_id 
0

試試這個,這將總結您就到forum_id forum_views。

SELECT forum_id 
    , SUM(forum_views) 
FROM TABLE_NAME 
GROUP BY forum_id 
ORDER BY 1 
2

使用此查詢select forum_id, sum(distinct forum_views) from table group by forum_id;

在此查詢和不同會做不同的價值的總和,做按期望的列進行分組。

如果你想不管清晰度的所有值的總和,那麼只需使用sum(forum_views)

0

你也可以試用一下:

select distinct forum_id, sum(topic_views) 
from jforum_topics 
group by forum_id