2014-11-24 176 views
0

回答測驗我有兩個表這樣的總ID:如何計算在SQL

測驗

quizNum | quizName 
------------------ 
1  | chapter 1 
2  | chapter 2 
3  | chapter 3 

studentassessment

stuNum | quizNum | assessmentMark 
----------------------------------- 
2012 | 1  | 10 
2012 | 2  | 8 
2013 | 1  | 10 
2014 | 1  | 5 

我想要得到的結果像這樣的東西

quizNum | quizName  | attempedstudent 
-------------------------------------------- 
1  | chapter 1  | 3   
2  | chapter 2  | 1   
3  | chapter 3  | 0   

描述:全2臺連接..我想計算已通過quziNum

完成了他們的測驗共有學生,這是典型的組合,我試圖:

$sql = "select a.quizNum, (count(a.stuNum)) as attemptedstudent ,c.quizName,c.quizDateFrom from studentassessment a join quiz c on a.quizNum=c.quizNum"; 

我寫這樣的,結果顯示它統計所有stuNum(學生)。是我不得不用另一種方式如獨特?

回答

0

您需要left joinQuizstudentassessment以確保Quiz的所有數據都與計數一起顯示。

select 
q.quizNum, 
q.quizName, 
count(sa.stuNum) as attempedstudent 
from Quiz q 
left join studentassessment sa on sa.quizNum = q.quizNum 
group by q.quizNum; 
+0

thanks..this is works .. – Lisa 2014-11-24 16:47:25