MySQL 在子查询中使用查询中的列时出错

MySQL error when using a column from a query in a subquery

我有以下代码:

select count(*) as points
from question
where (select count(*) 
       from answer inner join answer_test
       on answer_test.id_r = answer.id_r 
           and answer_test.right = answer.right 
           and  question.id_i=answer.id_i ) = 4

我收到这个错误:

#1054 - Unknown column 'question.id_i' in 'on clause' How should I access question.id_i in the subquery without getting that error?

我需要计算考试成绩。每个问题都有 4 种可能的答案,所以如果你答对了,你就会得到一分。 'Right' 列是一个布尔值,question.id_i 必须与 answer.id_i 匹配,但它表示即使它确实存在也无法识别。我应该如何修改它才能使其正常工作?

select count(*) as points
from question as q
where (select count(*) 
       from answer inner join answer_test
       on answer_test.id_r = answer.id_r 
           and answer_test.right = answer.right 
           and  q.id_i=answer.id_i ) = 4

我不确定,但请将您的条件与 question table 放在 where 子句中,如下所示:

select count(*) as points
from question
where (select count(*) 
       from answer inner join answer_test
       on answer_test.id_r = answer.id_r 
           and answer_test.right = answer.right 
           Where question.id_i=answer.id_i ) = 4 -- condition in where