MySQL 数据库在计数或求和和分组时失败 - 不是其他线程的重复
Posted
技术标签:
【中文标题】MySQL 数据库在计数或求和和分组时失败 - 不是其他线程的重复【英文标题】:MySQL database failing on count or sum and group by - not a duplicate of other threads 【发布时间】:2021-11-05 03:52:02 【问题描述】:我真的花了一周的时间在这里搜索并玩弄答案/方法......
db 是 mysql 5.7 - 没有机会去 MySQL8 它是一个拥有大量用户的学习管理数据库。
此查询搜索他们的学习记录,并返回(正确的)1 或 0,说明他们是否通过了课程。虽然看起来很复杂,但这很好用!
SELECT
CASE
/* first check if the number of completions in the scorm tracking table is zero */
WHEN COUNT(CASE WHEN t1.element = 'cmi.core.lesson_status' AND (t1.value = 'passed' OR t1.value = 'completed') THEN 1 ELSE NULL END) = 0 THEN 0
/* modulus checks if it is a correctly completed course */
WHEN (MOD(COUNT(CASE WHEN t1.element = 'cmi.core.lesson_status' AND (t1.value = 'passed' OR t1.value = 'completed') THEN 1 ELSE NULL END), (SELECT COUNT(sc9.id) AS scoes FROM mdl_scorm AS s9 LEFT JOIN mdl_scorm_scoes AS sc9 ON s9.id = sc9.scorm WHERE s9.course = c1.id AND sc9.launch = ''))) > 0 THEN 0
ELSE 1
END AS learningresult
FROM mdl_user AS u1
LEFT JOIN mdl_scorm_scoes_track AS t1 ON t1.userid = u1.id
LEFT JOIN mdl_scorm AS s1 ON s1.id = t1.scormid
LEFT JOIN mdl_course AS c1 ON c1.id = s1.course
LEFT JOIN mdl_scorm_scoes AS sc1 ON s1.id = sc1.scorm
WHERE u1.id = 2446345
AND sc1.launch = ''
GROUP BY c1.id, t1.attempt
对于示例用户(在本例中为 id = 2446345),这将返回一个名为 learningresult 的列,其中包含一系列 1 或 0 的行来表示通过或失败。
正如我所说,上面的代码运行良好......
但是,我想做的是对学习结果列进行计数或求和以返回他们通过的课程总数。
我试过了
SELECT COUNT(
CASE
WHEN etc
和
SELECT SUM(
CASE
WHEN etc
两者都返回 MySQL 错误 - 代码 1111 对 group by 函数的无效使用(并且此代码的大多数错误在 WHERE 子句中都有聚合!!)
这是非工作代码
SELECT COUNT(CASE WHEN refname=1 THEN 1 ELSE NULL END) AS complete
FROM
(SELECT CASE
/* first check if the number of completions in the scorm tracking table is zero */
WHEN COUNT(CASE WHEN t1.element = 'cmi.core.lesson_status' AND (t1.value = 'passed' OR t1.value = 'completed') THEN 1 ELSE NULL END) = 0 THEN 0
/* modulus checks if it is a correctly completed course */
WHEN (MOD(COUNT(CASE WHEN t1.element = 'cmi.core.lesson_status' AND (t1.value = 'passed' OR t1.value = 'completed') THEN 1 ELSE NULL END),
(SELECT COUNT(sc9.id) AS scoes FROM mdl_scorm AS s9 LEFT JOIN mdl_scorm_scoes AS sc9 ON s9.id = sc9.scorm WHERE s9.course = c1.id AND sc9.launch = ''))) > 0 THEN 0
ELSE 1
END AS refname
FROM mdl_user AS u1
LEFT JOIN mdl_scorm_scoes_track AS t1 ON t1.userid = u1.id
LEFT JOIN mdl_scorm AS s1 ON s1.id = t1.scormid
LEFT JOIN mdl_course AS c1 ON c1.id = s1.course
LEFT JOIN mdl_scorm_scoes AS sc1 ON s1.id = sc1.scorm
WHERE u1.id = 2446345
AND sc1.launch = ''
GROUP BY c1.id, t1.attempt) AS tblname
我查看了 ROLLUP,但没有使用此代码制作示例。
我已经将这项工作作为子查询...但是上面的这个工作查询已经是另一个的子查询,并且有一个子子查询使得用户 ID (2446345) 不可能由外部查询动态生成.
非常感谢您的关注 克里斯
【问题讨论】:
【参考方案1】:请阅读您的问题。
SELECT COUNT(*)
CASE
/* first check if the number of completions in the scorm tracking table is zero */
WHEN COUNT(CASE WHEN t1.element = 'cmi.core.lesson_status' AND (t1.value = 'passed' OR t1.value = 'completed') THEN 1 ELSE NULL END) = 0 THEN 0
/* modulus checks if it is a correctly completed course */
WHEN (MOD(COUNT(CASE WHEN t1.element = 'cmi.core.lesson_status' AND (t1.value = 'passed' OR t1.value = 'completed') THEN 1 ELSE NULL END), (SELECT COUNT(sc9.id) AS scoes FROM mdl_scorm AS s9 LEFT JOIN mdl_scorm_scoes AS sc9 ON s9.id = sc9.scorm WHERE s9.course = c1.id AND sc9.launch = ''))) > 0 THEN 0
ELSE 1
END AS learningresult
FROM mdl_user AS u1
LEFT JOIN mdl_scorm_scoes_track AS t1 ON t1.userid = u1.id
LEFT JOIN mdl_scorm AS s1 ON s1.id = t1.scormid
LEFT JOIN mdl_course AS c1 ON c1.id = s1.course
LEFT JOIN mdl_scorm_scoes AS sc1 ON s1.id = sc1.scorm
WHERE u1.id = 2446345
AND sc1.launch = ''
GROUP BY c1.id, t1.attempt
你为什么不这样尝试?
【讨论】:
请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。 嗨。谢谢,但这会计算已评估的 ROWS,而不是 learningresult 列的 1 和 0。 如果要检索 learningresult 为 1 或 0 的行,可以添加条件。比如 WHERE learningresult = "1"。以上是关于MySQL 数据库在计数或求和和分组时失败 - 不是其他线程的重复的主要内容,如果未能解决你的问题,请参考以下文章
mysql中,先分组,按某个字段计数,然后把计算出的数求和,怎么写,