简体   繁体   English

将我的两个查询合并为一个查询

[英]Combine my two queries into one query

I want to combine these two queries in one query: 我想将这两个查询合并到一个查询中:

SELECT s.student_id, s.name,
COUNT(a.student_id) as count
FROM students s
LEFT JOIN absences a ON a.student_id = s.student_id
GROUP BY a.student_id, s.name
ORDER BY count DESC


SELECT s.unitid, s.unitname,
COUNT(a.unitid) as count
FROM subjects s
LEFT JOIN absences a ON a.unitid = s.unitid
GROUP BY s.unitid, s.unitname
ORDER BY count DESC");

This is what the first query shows: 这是第一个查询显示的内容:

Student ID  Student Name    ABSENCES
5       donald duck          21
3       safedin smith    13
6       ace ventura         11

This is what i want it to show when i combine them 这是我希望将它们组合时显示的内容

Student ID Unit ID  Unit Name   Student Name           ABSENCES
5       1       history     donald duck              21
3       2       maths       safedin smith               13
6       3       Crap        ace  ventura               11

So as a conclusion the first query counts the total absences of a student, I want to make it count the absences of a student for each unit: 因此,作为结论,第一个查询计算的是学生缺勤的总数,我想对每个单元的学生缺勤进行统计:

my absences table has the following columns: 我的缺席表包含以下列:

absence_id Ascending
student_id
date
subject
unitid

my subjects table has the following columns(which means this is the table with the units): 我的Subjects表具有以下列(这是带有单位的表):

unitid
unitname

Edited query. 编辑查询。

SELECT s.student_id, sub.unitid, sub.unitname, s.name,
COUNT(a.student_id) as count
FROM students s
LEFT JOIN absences a ON a.student_id = s.student_id 
LEFT JOIN subjects sub ON a.unitid = sub.unitid
GROUP BY s.student_id, sub.unitid, sub.unitname, s.name
ORDER BY count DESC

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

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