具有不同行的列的计数

时间:2010-08-26 23:36:09

标签: sql mysql join count

我想加入两张桌子(下方)。但是,我想做一个计数,看看当contactId>时每个job_category显示多少次。 0

任何帮助将不胜感激!

表1:

| JobPositionId | JobPositionName | JobDescriptionId | JobCategoryId | ContactId
---------------------------------------------------------------------------------
       1        |  Audio Cables   |      1           |      1        |     1
       2        |Audio Connections|      2           |      1        |     1
       3        |Audio Connections|      2           |      1        |     0
       4        |   Sound Board   |      3           |      1        |     0
       5        |   Tent Pen      |      4           |      3        |     0

表2:

JobCategoryId | JobCategoryName
 --------------------------------
      1       |  Audio Setup
      2       |  Stage Setup
      3       |  Tent Setup

2 个答案:

答案 0 :(得分:1)

这样的东西
select jc.JobCategoryName, count(*) 
from JobCats jc inner join JobPostings jp on jc.JobCategoryId = jp.JobCategoryId
where jp.ContactId > 0
group by jp.JobCategoryId

答案 1 :(得分:1)

这会给你你想要的东西吗?

SELECT Table2.JobCategoryId, Table2.JobCategoryName, COUNT(Table2.JobCategoryId)
FROM Table1
INNER JOIN Table2 ON Table1.JobCategoryId = Table2.JobCategoryId
WHERE Table1.ContactId > 0
GROUP BY Table2.JobCategoryId, Table2.JobCategoryName