根据相应条件统计需要的count
查询总count数(totalCount)
计算百分比:count / totalCount * 100
表设计:
CREATE TABLE `topic_exercise` (`id` bigint(11) NOT NULL,`topic_id` bigint(11) NOT NULL COMMENT '试题id',`user_id` bigint(11) NOT NULL COMMENT '用户id',`user_answ` varchar(255) NOT NULL,`topic_answ` varchar(255) DEFAULT NULL COMMENT '试题答案',`serial` bigint(11) NOT NULL COMMENT '用户练习模块答题记录序号',`records_id` bigint(11) DEFAULT NULL COMMENT '练习记录列表id',`status` tinyint(4) DEFAULT NULL COMMENT '0 错误 1正确',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
SQL写法:
SELECTROUND(T1.co / T2.totalCo * 100, 1) as accuracy
FROM(SELECTCOUNT(*) AS coFROMtopic_exerciseWHEREuser_id = 844228520938668032AND user_answ=topic_answ) T1,(SELECTCOUNT(*) AS totalCoFROMtopic_exerciseWHEREuser_id = 844228520938668032) T2;
查询结果: