好吧,我正在尝试抓住教师团队领导并列出他们所关联的项目ID。但是,如果在与该项目ID相关的课程中注册了至少2名学生,我只想添加项目ID。
这就是我所拥有的:
#Distinct students enrolled in CI
SELECT DISTINCT c.*,
(
#Projects in ci_id order with students enrolled in the courses
SELECT GROUP_CONCAT(DISTINCT ci_projects.ci_id SEPARATOR ', ')
FROM `ci_projects` LEFT JOIN `ci_team_leaders` ON ci_projects.ci_id=ci_team_leaders.ci_id WHERE ci_team_leaders.username=c.username
) AS CIs
FROM `ci_projects` AS x
INNER JOIN `ci_team_leaders` AS b ON x.ci_id=b.ci_id
LEFT JOIN `ci_users` AS c ON b.username=c.username
LEFT JOIN `departments` AS d ON c.dept_id=d.dept_id
LEFT JOIN `colleges` AS e ON d.college_id=e.college_id
WHERE x.status = 2 GROUP BY c.username HAVING (SELECT COUNT(`reg_username`) FROM `all_course_reg` AS z INNER JOIN `ci_courses` AS y ON y. course_crn=z.reg_crn WHERE y.ci_id =ci_id AND z.reg_term = '201308') > 1
查询运行,但HAVING部分不会改变结果。
好吧,我想我知道了......
SELECT DISTINCT `username` AS user, (SELECT GROUP_CONCAT(DISTINCT a.ci_id SEPARATOR ', ')
FROM ci_team_leaders AS a
LEFT JOIN `ci_courses` AS b ON a.ci_id=b.ci_id
LEFT JOIN `all_course_reg` AS c ON b.course_crn=c.reg_crn
WHERE a.username=user AND (SELECT COUNT(`reg_username`) FROM `all_course_reg` AS z WHERE z.`reg_crn`=c.reg_crn) > 1) AS CIs FROM `ci_team_leaders` WHERE CIs IS NOT NULL