2017-09-06 136 views
-1

我有一個MySQL表格,我想從中提取出席信息(學生ID,課程/主題出席,日期範圍,學生是否在場)。我寫了下面的查詢:多個PHP子查詢沒有給出預期的結果

SELECT 
    COUNT(a_id), 
    (
    SELECT COUNT(*) FROM attendance 
    WHERE state = 'present' 
     AND `dater` BETWEEN '$a' AND '$b' 
) AS Count, 
    stud_id 
FROM attendance 
WHERE 
    stud_id =(SELECT id FROM users WHERE NAME = '$stud') 

這是給我正確的結果,但是當我改變了學生,它不是給我的記錄本的日子正確的計數。不提,我已經沒有添加過程參數到查詢

enter image description here enter image description here

MySQL表如下: enter image description here

我需要幫助的查詢返回所期望的結果(爲每個學生計算準確的日期,並在查詢中添加課程參數,以便查詢將查找特定課程的出勤記錄,特定學生的指定日期範圍內的出勤記錄)。

回答

0

看起來要單獨您的疑問:

Select (select count(*) from <database>.attendance where state = 'present' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as present, (select count(*) from <database>.attendance where state = 'absent' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as absent from <database>.attendance WHERE stud_id =(SELECT id FROM users WHERE NAME = '$stud'); 

試試這個:)

0

解決它使用連接如下:

SELECT u.id, a.stud_id, a.course_id, count(*) FROM attendance a 
JOIN users u ON u.id=a.stud_id 
JOIN courses c ON c.c_id=a.course_id 
WHERE a.state='present' and dater between '2017-09-01' and '2017-09-14' 
GROUP BY a.stud_id, a.course_id; 

感謝您的幫助。

相關問題