2013-11-26 83 views
0

我在加入2個查詢時遇到問題,但在第二個查詢中我只想引入計數。用mysql加入計數查詢

這首先查詢工作以及

SELECT DISTINCT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, members.first_name, views 
FROM forum_sub, members 
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC 

其中顯示以下

---------------------------------------------------------------------- 
sub_id | forum_id | title | text | url | userid | first_name | views | 
---------------------------------------------------------------------- 
    20 | 1  | test | test |test | 1001 | JOhn  | 123 | 
---------------------------------------------------------------------- 
    1 | 1  | test | test |test | 1002 | Pete  | 23 | 
---------------------------------------------------------------------- 
    10 | 1  | test | test |test | 1003 | Harry  | 34 |                
---------------------------------------------------------------------- 

但現在我想加入上述sub_id到另一臺名爲forum_topics和計算有多少相同sub_id的存在的並帶上那個價值

例如我可以使用

SELECT sub_id,COUNT(sub_id)爲主題FROM forum_topics GROUP BY sub_id

----------------- 
|sub_id | topics| 
    --------------- 
| 1 | 4 | 
----------------- 
| 10 | 3 | 
----------------- 
| 20 | 5 | 
----------------- 

我的問題是如何能夠加入那些2個查詢,所以我得到這樣的

---------------------------------------------------------------------------- 
sub_id | forum_id | title | text | url | userid | first_name | views | count| 
----------------------------------------------------------------------------- 
    20 | 1  | test | test |test | 1001 | JOhn  | 123 | 5 | 
----------------------------------------------------------------------------- 
    1 | 1  | test | test |test | 1002 | Pete  | 23 | 4 | 
----------------------------------------------------------------------------- 
    10 | 1  | test | test |test | 1003 | Harry  | 34 | 3 |              
----------------------------------------------------------------------------- 

任何幫助將是巨大的,我知道我需要使用子查詢,但我一直停留在這個幾乎所有的一天,沒有運氣

+0

你在使用論壇主題? 'where'子句可能需要根據'from'子句進行一些更改。 –

+0

forum_topics不應該在頂級查詢中真正需要,但forum_topics是sub_id的計數所在的表格,例如可能有10條關於該主題的評論,我希望它可以統計每個主題的數量。 – TRicks

回答

0

儘可能避免SELECT DISTINCT。它是邪惡的。它會隱藏您在查詢中存在的交叉連接等。從這個查詢中很難分辨出你在做什麼。

但是,包括你有兩個選擇計數:

一個是做選擇查詢子:

SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, 
members.first_name, views , (SELECT COUNT(sub_id) as topics FROM forum_topics WHERE sub_id = forum_sub.sub_id) count 
FROM forum_sub, members 
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC 

另一個辦法是真正參與到子查詢你創建:

SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, 
     members.first_name, views, counts.topics 
FROM forum_sub, members 
JOIN (SELECT sub_id, COUNT(sub_id) as topics FROM forum_topics GROUP BY sub_id) counts  ON (counts.sub_id = forum_sub.sub_id) 
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC 

你也應該看看加入,而不是從

0123選擇在多個表
SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, 
     members.first_name, views, counts.topics 
FROM forum_sub 
JOIN members ON (members.userid = forum_sub.userid) 
JOIN (SELECT sub_id, COUNT(sub_id) as topics FROM forum_topics GROUP BY sub_id) counts ON (counts.sub_id = forum_sub.sub_id) 
WHERE forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC 
+0

哇,你是一個非常聰明的傢伙,謝謝你,謝謝你,很好的解釋 – TRicks

0

嘗試

group by sub_id 

在第二個查詢的結尾,而不是WHERE。 然後將它加入到其他表中。

+0

我無法得到這個工作,我編輯了代碼,使其更易於解釋 – TRicks