[英]Mysql how to connect 2 subqueries
我有這個 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
包括distinct
關鍵字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.