2012-12-02 87 views
0

我想在我的課程管理系統中創建講師的月度出席報告。在這個報告中,我需要使用嵌套的select查詢來計算講師的在場和缺席的數量,以便在特定記錄中顯示他們。 我認爲如果講師代碼與主查詢中正在處理的講師代碼相同,則可以使用select語句對每個出席狀態進行計數!加入內部選擇查詢與主要查詢

select lecturers.*, sum(distinct courses.credit) as 'Due Credits',(select count(*) from attendances where status='1') as Present, (select count(*) from attendances where status='0') as Absent from lecturers,courses,attendances 
where attendances.lecturer_code=lecturers.code 
AND courses.lecturer_code=lecturers.code 
group by lecturer_code 

[代碼,名稱,部門,官職,學位固定屬性]

我需要知道,如果有更好的方式來做到這一點!

非常感謝!

+1

請張貼表的結構,一些樣本數據和所需的輸出。 –

回答

0
SELECT lecturers.*, 
     SUM(DISTINCT courses.credit) AS `Due Credits`, 
     SUM(attendances.status = 1) AS `Present`, 
     SUM(attendances.status = 0) AS `Absent`, 
FROM  lecturers 
    JOIN courses  ON  courses.lecturer_code = lecturers.code 
    JOIN attendances ON attendances.lecturer_code = lecturers.code 
GROUP BY lecturer_code