2009-12-28 125 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 = ?