2011-08-26 81 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

你,我没有想过集团,并且......我的失败...... –