[英]SQL sub queries
I need to get (count of completed modules) along with (count of course modules) in a (course) for an (user) 我需要为(用户)在(课程)中获得(完成模块的数量)和(课程模块的数量)
Tables: 表:
course_modules - will have all the course modules
course_modules_completion - will have all the users with completed modules.
I want to show all the three in one query like example: 我想在一个查询中显示所有三个示例,例如:
(course) (count of course modules) (count of completed modules) (userid)
3 20 5 12
5 20 8 7
Here is the query I tried : 这是我尝试的查询:
SELECT
c.fullname, count(cm.id) AS "coursemodules", cmc.userid,
(SELECT count(cmc.coursemoduleid)
FROM mdl_course c
JOIN mdl_course_modules cm
JOIN mdl_modules m
JOIN mdl_course_modules_completion cmc ON c.id = cm.course
AND cm.module = m.id
AND cmc.coursemoduleid = cm.id
AND cmc.userid = 12
AND cm.course = 7
AND cmc.completionstate BETWEEN 1 AND 10) AS "completedmodules"
FROM
mdl_course c
JOIN mdl_course_modules cm
JOIN mdl_modules m
ON c.id = cm.course AND cm.module = m.id AND cm.course = 7
It shows an error: 它显示一个错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT count(cmc.coursemoduleid) FROM mdl_course c JOIN mdl_course_modules cm JO' at line 3
Could anyone suggest me? 有人可以建议我吗?
Data for course_modules
id course moduletypeid
1 3 9
2 3 9
3 3 17
4 5 20
5 5 18
Data for course_modules_completion
id coursemoduleid userid
1 153 12
2 154 12
3 155 11
4 120 12
5 167 11
Should be 应该
FROM mdl_course c
JOIN mdl_course_modules cm ON c.id = cm.course
JOIN mdl_modules m ON cm.module = m.id
JOIN mdl_course_modules_completion cmc ON
cmc.coursemoduleid = cm.id
WHERE
AND cmc.userid = 12
AND cm.course = 7
AND cmc.completionstate BETWEEN 1 AND 10
UPDATE UPDATE
You can try to get rid of the subquery in SELECT and replace it with 您可以尝试摆脱SELECT中的子查询,并将其替换为
SUM(if (cmc.completionstate BETWEEN 1 AND 10,1,0) AS "completedmodules"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.