2014-11-16 52 views
0

該數據庫包含一個REGISTRATION表和STUDENT表。如何從SQL中的列中選擇不同數量的值?

REGISTRATION表的列是:

CourseID, StudentID, CourseCode, Score, Year 

CourseCode列包含的像CS-101課程代碼,MS-202(每個學生ID被註冊到許多課程)。我需要找到超過3門課程的學生的姓名和身份證。

我曾嘗試:

Select distinct 
    CourseRegistrations.S_ID, Students.FirstName 
from 
    Students, CourseRegistrations 
where 
    Students.StudentID = CourseRegistrations.StudentID 
group by 
    CourseRegistrations.S_ID, Students.FirstName 
having 
    count(distinct CourseRegistrations.CourseCode) > 3 

,但這正顯示出文件中的所有記錄。

+0

[不良習慣踢:使用舊版本yle JOINs](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins.aspx) - 舊式* comma-在ANSI - ** 92 ** SQL標準(**超過20年**之前)中,* *樣式的分隔列表被替換爲* proper * ANSI'JOIN'語法,並且不鼓勵使用它 –

回答

0

Distinct並不需要,如果你使用GROUP BY

Select CusReg.S_ID 
from Students stud join CourseRegistrations CusReg 
ON stud.StudentID=CusReg.StudentID 
GROUP BY CourseRegistrations.S_ID 
having count(CusReg.CourseCode)>3 

不使用舊式加入

SOURCE

0

你應該嘗試:

Select CourseRegistrations.StudentID, count(CourseRegistrations.CourseCode) 
from Students, CourseRegistrations 
where Students.StudentID=CourseRegistrations.StudentID 
GROUP BY CourseRegistrations.StudentID 
having count(CourseRegistrations.CourseCode)>3 
相關問題