【问题标题】:SQL: Combine two queries into a single oneSQL:将两个查询合并为一个
【发布时间】:2016-04-22 09:45:36
【问题描述】:

我有以下两个查询:

    Select count(project.id) as num_project , 
           industry.name as industry_name 
    from   project, project_industry, industry 
    where  industry.id= project_industry.industry_id and 
           project.id = project_industry.project_id  
    group by industry.id;

Select count(user.id) as num_consultants , 
       industry.name as industry_name 
from   consultant_profile, industry_experience,user, industry 
where  industry_experience.consultant_profile_id = consultant_profile.id
       and industry_experience.industry_id= industry.id 
       and user.type=0 
       and user.is_active=1 
       and consultant_profile.user_id=user.id 
group by industry_id;

我试图将它们组合成一个,如下所示:

SELECT i.name AS industry_name, num_projects, num_consultants
FROM   industry i
JOIN   (SELECT   pc.industry_id, COUNT(p.id) AS num_projects
        FROM     project p
        JOIN     project_industry pc ON p.id = pc.project_id
        GROUP BY pc.industry_id) x ON x.industry_id = i.id
JOIN   (SELECT   y.industry_id, COUNT(u.id) AS num_consultants
        FROM     user u, consultant_profile cp
        JOIN     project_experience pe on pe.consultant_profile_id = cp.id
        WHERE    u.is_active = 1 AND u.type = 0
        GROUP BY y.industry_id) z ON z.industry_id = i.id;

但它似乎不起作用。谁能指出我做错了什么?非常感谢任何帮助。

编辑:为了清楚起见,第一个查询显示与每个行业相关的项目数量。第二个查询显示与每个行业相关的顾问数量。

我希望第三个查询在同一个表的 3 个单独列上显示前两个查询的信息:行业名称、#Projects、#Consultants

表结构如下,其中 -> 描述了表的主键和外键:

Project -> project.id
Project_industry -> project_industry.id, project_id, industry_id
Industry -> industry.id

user -> user.id
Consultant_profile -> consultant_profile.id, user_id
Industry_Experience -> industry_experience.id, consultant_profile_id, industry_id
Industry -> industry.id

【问题讨论】:

  • 你能告诉我们你的表结构和你想得到的输出吗?
  • 合并如何?向我们展示您要合并的两个查询中的几行,以及合并后的结果。
  • 您确定您的第二个查询是正确的吗?请交叉检查'i'是否是任何表的别名
  • 你可以使用 union all
  • 究竟是什么不起作用?你有什么错误吗?顺便说一句,在您的选择语句中,它应该是 SELECT i.name AS industry_name, x.num_projects, z.num_consultants - 添加别名。

标签: mysql sql


【解决方案1】:

你快到了。根据我的理解,下面的查询应该适合你。

查询

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

【讨论】:

  • 感谢您的回复,不幸的是,您的实施给了我以下错误:错误代码:1054。“on 子句”中的未知列“T1.industry.id”
  • @MarcZaharescu,此查询是否终止?因为它看起来一样,但语法略有不同。
猜你喜欢
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
相关资源
最近更新 更多