Mysql如何连接2个子查询

标签 mysql sql

我有这个 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

我的代码在 mysql 工作台中返回 2 个不同的结果集。

有什么方法可以连接这两个结果集吗?

最佳答案

您可以在三个表之间使用双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

包括不同关键字。

关于Mysql如何连接2个子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59164515/

相关文章:

sql - PostgreSql 函数 "Insert data from a table to an other"

php - mysql 不同的查询

php - 根据条件创建mysql更新字符串

Mysql - 模拟先前多行(不仅仅是一行)的滞后

mysql - 自动删除连接数据以及删除记录?

mysql - 从每个成员最好的 2 个分数中获取总分

mysql - 更新或插入后mysql什么时候更新它的索引树?

javascript - UTF8 编码/€

java - 如何编写一个 SQL 以便它可以在任何数据库上运行

MySQL - 查询组合具有相同 ID 的行并保留该 ID 的所有条目但作为一条记录