简体   繁体   中英

Need mySql query

I have n number of users with different articleId .
If I pass the articleId , I want to get the results like how many different users viewed that article.

Table 1:

recId   userId   articleId    
-----   ------    --------
100      1001      1   
103      178       2   
106      475       3   
107      327       4   
108      567       5   
109      568       6   

Table 2:

userId     jobtitle        articleId
-----       ------          --------  
  327       Engineer          4
  178       Professor         4
  475       Doctors           4
  327       Engineer          5
  568       Student           6
  475       Doctors           4
  475       Doctors           8

If I pass the articleId as 4 , It should return the details like how many times that particular article viewed by different users like:

jobtitle   total
-----      ------    
Doctors     2
Engineer    1
Professor   1

if i pass articleid as 6 , result will be like this:

jobtitle   total
-----      ------    
Student      1

How to write this mysql script?

select jobtitle, count(articleid)
from table2 
where articleid = YOUR_ARTICLE_ID
group by jobtitle
SELECT jobtitle, COUNT(*) as count
FROM Table2
WHERE articleId = 4
GROUP BY jobtitle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM