2011-08-26 105 views
0

Select查詢我有一個輸入的數據, 有兩個表SQL:沒有嵌套查詢

create table Courses(
id number(19) not null, 
name varchar(100), 
); 

create table Students (
id number (19) not null, 
name varchar(100), 
course_id number (19) not null, 
); 

我需要編寫查詢得到所有cources與學生例如數超過10 所以,我受夠了嵌套查詢寫入變種這樣

select distinct courses.name from student, courses where courses.id=student.courses_id and (select count(Students.id) from Students where student.course_id = course.id) > 10 

!沒有測試,只是寫這篇文章,它是例子! 所以,我的問題是如何編寫相同的查詢沒有嵌套查詢?

回答

2

使用GROUP BY/HAVING條款:

SELECT courses.name /*, COUNT(*)*/ 
FROM student JOIN courses ON courses.id = student.courses_id 
GROUP BY courses.id 
HAVING COUNT(*) > 10 
+0

謝謝你,我沒有想過group by和having ...我...失敗謝謝 –

2

我會去與

Select c.name, count(s.id) as StudentsCount from Courses c join stundets S on c.id = s.course_id group by c.name having count(s.id) > 10 
+0

你,我沒有想過集團,並且......我的失敗...... –