日期:2014-05-16  浏览次数:20867 次

SQL如何分类组合查询?
现有表
course,列为course_uid,course_name
enrolled,列为course_uid,student_uid

现在想将每个COURSE_UID分别由STUDENT_UID数列出来,同时列出相应COURSE_UID对应的COURSE_NAME值

我尝试写成

SQL code


SELECT Course_Name, (

SELECT count( * )
FROM `enrolled`
GROUP BY Course_Uid
)
FROM course
WHERE Course_Uid
IN (

SELECT Course_Uid
FROM enrolled
GROUP BY Course_Uid
);


但无法得到想要的值,SQL提示Subquery returns more than 1 row

------解决方案--------------------
SQL code

select a.course_uid,a.course_name,count(b.student_uid) cout
from course a,enrolled b 
where a.course_uid=b.course_uid
group by a.course_uid;