求一个Sql的写法(可加分),兄弟们帮帮忙

fishzone 2001-11-27 09:56:01
有这么一个学生答案表: studentanswer
student_id test_id question_id model_answer student_answer
0000000001 00001 1 A A
0000000002 00001 1 A B
.
.
.
.
想在我想统计同一次考试的每一个题目有多少人答对了。用一个Sql写完,应该怎么写?
如果用n(n为question_id的个数)条sql语句,可以这样写:
select count(student_answer) as countanswer from studentanswer where test_id=1 and question_id=1 and model_answer=student_answer
不想这样写,是因为如果n很大的话,这样会很慢。
...全文
171 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fishzone 2001-11-27
  • 打赏
  • 举报
回复
问题解决,谢谢各位。特别是 karma(无为) :)
但是我用的数据库是access,所以我把你的语句改成:
select question_id, sum(iif(model_answer=student_answer,1,0))
from studentanswer
group by question_id

case wher ... then ...else 在这里不行,它好像是用在Sql server中的
fishzone 2001-11-27
  • 打赏
  • 举报
回复
karma(无为):
试一下,谢谢。
karma 2001-11-27
  • 打赏
  • 举报
回复
select question_id, sum( case when model_answer=student_answer then 1 else 0 end )
from studentanswer
group by question_id
shkingstargroup 2001-11-27
  • 打赏
  • 举报
回复
同一次考试的每一个题目有多少人答对了
select question_id, count(student_id)
from studentanswer
where model_answer=student_answer and test_id=00001
group by question_id
fishzone 2001-11-27
  • 打赏
  • 举报
回复
karma(无为):
你把 where model_answer=student_answer 放在了group by 之前,如果没有正确答案的话,它将返回不了这个question_id 下的 recordset而不是countanswer=0
lisbjing 2001-11-27
  • 打赏
  • 举报
回复
to karma:
你明白他的意思嗎?不知道他要什麼?
select question_id, count(student_id)
from studentanswer
where model_answer=student_answer
group by question_id
karma 2001-11-27
  • 打赏
  • 举报
回复
the answer I provided is exactly what you want
fishzone 2001-11-27
  • 打赏
  • 举报
回复
谢谢你的关注,我本来想这样写:
select count(student_answer) as countanswer from studentanswer where test_id=00001 group by question_id
这样可以返回每一个题目有多少个人做答,但是,怎样才可以返回每一道题有多少个((正确))答案呢?
karma 2001-11-27
  • 打赏
  • 举报
回复
select question_id, count(student_id)
from studentanswer
where model_answer=student_answer
group by question_id
foolishtiger 2001-11-27
  • 打赏
  • 举报
回复
用存储过程会快很多.就SQL语句的写法来说,你的思路是对的.

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧