如何组合加入这两个表?

时间:2010-08-28 04:25:43

标签: mysql join

如何组合加入这两个表?

表1

SELECT job_category.JobCategoryId, job_category.JobCategoryName, count(job_position.JobCategoryId) 
AS AvailableCategories
FROM job_position 
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName

表2

(SELECT job_category.JobCategoryId, job_category.JobCategoryName, count(job_position.ContactId) AS AllocatedJobs
FROM job_position 
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
WHERE job_position.ContactId > 0
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName)

由于

1 个答案:

答案 0 :(得分:0)

我认为应该是这样的

Select T1.JobCategoryId, T1.JobCategoryName, T1.AvailableCategories, T2.AllocatedJobs    
FROM    
 (SELECT job_category.JobCategoryId as JobCategoryId, job_category.JobCategoryName as JobCategoryName, count(job_position.JobCategoryId) 
AS AvailableCategories
FROM job_position 
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName) as T1 

INNER JOIN 

((SELECT job_category.JobCategoryId as  T2JobCategoryId, job_category.JobCategoryName as T2JobCategoryName, count(job_position.ContactId) AS AllocatedJobs
FROM job_position 
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
WHERE job_position.ContactId > 0
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName)) as T2
ON T1.JobCategoryId = T2.T2JobCategoryId