简体   繁体   English

Mysql如何连接2个子查询

[英]Mysql how to connect 2 subqueries

I have this mysql code:我有这个 mysql 代码:

select 
    course, students
from
    (select 
         course_id, count(*) as students
     from 
         taughtby
     where 
         u in (select s_id from student where s_id = u)
     group by 
         course_id) as B;

select 
    course, professors
from
    (select 
         course_id, u_id, count(*) as professors
     from 
         taughtby
     where 
         u in (select p_id from professor where p_id = u)
     group by 
         course_id) as A

My code returns 2 different result set in mysql workbench.我的代码在 mysql 工作台中返回 2 个不同的结果集。

Is there any way I can connect those 2 result sets?有什么办法可以连接这两个结果集?

You can use double join clause among three tables :您可以在三个表中使用双join子句:

 select course_id, count(distinct p_id) as professors, count(distinct s_id) as students
   from taughtby
   left join professor on p_id = u_id
   left join student on s_id = u_id
  group by course_id

including distinct keyword.包括distinct关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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