将不同查询的输出合并为单个查询输出

时间:2018-09-08 04:48:02

标签: mysql sql monetdb

如何合并不同查询的输出。

我有以下带有输出的sql查询:

1) select d.departmentid, d.name, count(distinct(sg.personid)) as noOfStudents from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid group by d.departmentid, d.name;
+--------------+--------------+--------------+
| departmentid | name         | noofstudents |
+==============+==============+==============+
|          101 | Computer Sci |            1 |
|          104 | Mech         |            2 |
|          103 | EEE          |            1 |
+--------------+--------------+--------------+



2) select d.departmentid, d.name, count(distinct(ci.personid)) as noOfTeachers from department d inner join course c on c.departmentid = d.departmentid inner join course_instructor ci on ci.courseid = c.courseid group by d.departmentid, d.name;
+--------------+--------------+--------------+
| departmentid | name         | noofteachers |
+==============+==============+==============+
|          101 | Computer Sci |            1 |
|          103 | EEE          |            2 |
|          104 | Mech         |            1 |
|          102 | ECE          |            3 |
+--------------+--------------+--------------+

3) select d.departmentid, d.name, count(distinct(c.courseid)) as noOfCourses from department d inner join course c on c.departmentid = d.departmentid group by d.departmentid, d.name;
+--------------+--------------+-------------+
| departmentid | name         | noofcourses |
+==============+==============+=============+
|          101 | Computer Sci |           3 |
|          102 | ECE          |           3 |
|          104 | Mech         |           1 |
|          103 | EEE          |           2 |
+--------------+--------------+-------------+

现在,我想将所有三个组合到一个表中以显示数据。 我怎样才能做到这一点?

我在这里尝试UNION操作,是否很好用?

2 个答案:

答案 0 :(得分:0)

如果需要在输出中将 Students Teachers Courses 作为单独的列,您可以尝试执行以下操作:

select 
    d.departmentid, d.name, 
    count(distinct(sg.personid)) as noOfStudents, 
    0 as noofteachers, 0 as noofcourses 
from 
    department d 
inner join 
    course c on c.departmentid = d.departmentid 
inner join 
    student_grade sg on sg.courseid = c.courseid 
group by 
    d.departmentid, d.name

UNION ALL

select 
    d.departmentid, d.name, 0 as noOfStudents,
    count(distinct(ci.personid)) as noOfTeachers, 0 as noofcourses 
from 
    department d 
inner join 
    course c on c.departmentid = d.departmentid 
inner join 
    course_instructor ci on ci.courseid = c.courseid 
group by 
    d.departmentid, d.name

UNION ALL

select 
    d.departmentid, d.name, 0 as noOfStudents, 
    0 as noofteachers, count(distinct(c.courseid)) as noOfCourses 
from 
    department d 
inner join 
    course c on c.departmentid = d.departmentid 
group by 
    d.departmentid, d.name

这应该产生这样的内容:

+--------------+--------------+--------------+--------------+--------------+  
| departmentid | name         | noofstudents | noofteachers | noofcourses  |  
+==============+==============+==============+==============+==============+  
|          101 | Computer Sci |             1|             0|             0|  
|          104 | Mech         |             2|             0|             0|  
|          103 | EEE          |             1|             0|             0|  
|          101 | Computer Sci |             0|             1|             0|  
|          103 | EEE          |             0|             2|             0|  
|          104 | Mech         |             0|             1|             0|  
|          102 | ECE          |             0|             3|             0|  
|          101 | Computer Sci |             0|             0|             3|  
|          102 | ECE          |             0|             0|             3|  
|          104 | Mech         |             0|             0|             1|  
|          103 | EEE          |             0|             0|             2|  
+--------------+--------------+--------------+--------------+--------------+  

如果您需要汇总值,只需将整个查询括在方括号中,然后对结果求和

SELECT 
    departmentid, name, 
    SUM(noOfStudents), SUM(noOfTeachers), SUM(noOfCourses)
FROM 
    (Query1 UNION ALL Query2 UNION ALL Query3) AS mySubQuery
GROUP BY 
    departmentid, name

答案 1 :(得分:0)

您可以使用内部联接在同一行上获取所有结果

  select  t0.departmentid
        , t0.name
        , t1.noOfStudents
        , t2.noOfTeachers 
        , t3.noOfCourses 
  from department t0
  left join 
  (
  select d.departmentid
    , d.name
    , count(distinct(sg.personid)) as noOfStudents
  from department d 
  inner join course c on c.departmentid = d.departmentid 
  inner join student_grade sg on sg.courseid = c.courseid 
  group by d.departmentid, d.name
  ) t1 ON t0.departmentid = t1.departmentid
  left join (
    select d.departmentid
      , d.name
      , count(distinct(ci.personid)) as noOfTeachers 
    from department d 
    inner join course c on c.departmentid = d.departmentid 
    inner join course_instructor ci on ci.courseid = c.courseid 
    group by d.departmentid, d.name
  ) t2 on t0.departmentid = t2.departmentid 
  left join  (
    select d.departmentid
    , d.name
    , count(distinct(c.courseid)) as noOfCourses 
    from department d 
    inner join course c on c.departmentid = d.departmentid 
    group by d.departmentid, d.name
  ) t3 on t0.departmentid = t3.departmentid 

或者如果您需要单独行的结果,则可以使用UNION ALL