2009-12-28 122 views
0

我改變了我的線程,因爲我的最後一個線程也是這樣。但實際上這是我的要求。需要Mysql查詢(線程已更改)

我有n個不同的articleId用戶。 如果我通過articleId,我想要得到像有多少不同用戶查看該文章的結果。

表1:

 
recId userId articleId  
----- ------ -------- 
1  10111  102  
2  10121  102  
3  10111  102  
4  10121  105   
5  10111  102   
6  10122  105  

表2:

 
userId  jobtitle   
-----  ------    
10111  Engineer   
10121  Doctor 
10122  Professor    

如果我通過了條款ArticleID爲「102」,從表1,它應該返回像多少次特定文章由不同用戶查看詳情如:

 
jobtitle total 
-----  ------  
Engineer 3 
Doctor  1 
Professor 1 

如果我通過articleid爲「105」,結果將如下所示:

 
jobtitle total 
-----  ------  
Doctor  1 
Professor 1 

如何爲上述結果編寫查詢?

在此先感謝

  • Gnaniyar祖拜爾

回答

1
SELECT jobtitle, COUNT(*) AS total 
FROM table1 
JOIN table2 ON (table1.userId = table2.userId) 
WHERE articleId = <your articleid> 
GROUP BY table2.jobtitle 
+0

這是非常有幫助的大部頭彼得。非常感謝你和stackoveflow – 2009-12-31 20:24:37

1
SELECT 
    jobtitle, COUNT(recID) as total 
    FROM table1 a 
    INNER JOIN table2 b 
    ON 
    a.userId = b.userId 
    GROUP BY 
    jobtitle 
    WHERE articleId = ?