SQL: Joining two complex queries issue

时间:2016-04-25 08:56:28

标签: mysql sql join inner-join

I have the following query. Which aims to displays, project function, project industry, nr of projects and number of consultants associated with them. After introducing the project industry the query became a bit complex and I am unsure how to match the industry.name from the first inner query with the industry.name1 from the second inner query.

Any help would be much appreciated!

And this is the query:

SELECT 
    T1.job_function_name,
    industry_name,
    num_project,
    num_consultants
FROM
    (SELECT 
        COUNT(project.id) AS num_project,
            job_function.name AS job_function_name,
            industry.name AS industry_name,
            job_function.id AS id
    FROM
        project, project_jobfunction, job_function, project_industry, industry
    WHERE
        industry.id = project_industry.industry_id
            AND project.id = project_industry.project_id
            AND job_function.id = project_jobfunction.jobfunction_id
            AND project.id = project_jobfunction.project_id
    GROUP BY industry.name , job_function.id) T1
        JOIN
    (SELECT 
        COUNT(user.id) AS num_consultants,
            job_function.name AS job_function_name,
            industry.name1 AS industry_name1,
            job_function.id AS id
    FROM
        consultant_profile, job_function_experience, user, job_function, industry_experience, industry
    WHERE
        industry_experience.consultant_profile_id = consultant_profile.id
            AND industry_experience.industry_id = industry.id
            AND job_function_experience.consultant_profile_id = consultant_profile.id
            AND job_function_experience.job_function_id = job_function.id
            AND user.type = 0
            AND user.is_active = 1
            AND consultant_profile.user_id = user.id
    GROUP BY industry.name , job_function_id) T2 ON T1.id = T2.id;

0 个答案:

没有答案