【问题标题】:SQL subquery with reference to parent table gives "Unknown column 'test_results.id' in 'where clause'"参考父表的 SQL 子查询给出“'where 子句'中的未知列'test_results.id'”
【发布时间】:2021-04-21 03:03:04
【问题描述】:

首先,看一下图表(这是一个测试学生知识的应用程序)

我已经有工作应用程序,它计算分数(以百分比为单位),但要按分数排序,需要选择(当前测试的)所有记录。而且它大大减慢了应用程序的速度(大约 10 秒的等待时间)。所以我决定将该逻辑移到单个 sql 查询中。

现在,我的 SQL 查询如下所示:

select test_results.*,
       (
           select test_result_total_score * 100 / test_result_total_max_score
           from (
                    select (select sum(question_score)
                            from (
                                     select question_total_right_answers = question_total_options as question_score
                                     from (
                                              select (
                                                         select count(*)
                                                         from answers
                                                                  inner join answer_options on answer_options.id = answers.answer_option_id
                                                         where answers.asked_question_id = asked_questions.id
                                                           and answers.is_chosen = answer_options.is_right
                                                     ) as question_total_right_answers,
                                                     (
                                                         select count(*)
                                                         from answers
                                                                  left join answer_options on answer_options.id = answers.answer_option_id
                                                         where answers.asked_question_id = asked_questions.id
                                                     ) as question_total_options
                                              from asked_questions
                                              where asked_questions.test_result_id = test_results.id
                                          ) as rigt_per_question
                                 ) as questions_scores)                             as test_result_total_score,
                           (select count(*)
                            from asked_questions
                            where asked_questions.test_result_id = test_results.id) as test_result_total_max_score
                ) as right_per_test_result
       ) as result_in_percents
from test_results
where test_results.id between 1 and 200;

这是它应该做的:对于每个提出的问题,收集有多少答案选项 (question_total_options) 以及用户选择正确的答案 (question_total_right_answers) - 非常嵌套的子查询。 然后为每个结果计算score(如果用户选择了所有正确的选项,则基本上为 1,如果至少选择了一个选项不正确,则为 0)。 之后,我们将所有问题的scores 相加(test_result_total_score - 用户回答正确的问题数量)。此外,我们计算测试结果中有多少问题(test_result_total_max_score)。 有了这些信息,我们可以计算正确回答问题的百分比 (test_result_total_score * 100 / test_result_total_max_score)

并且错误出现在第 23 和 28 行:

where asked_questions.test_result_id = test_results.id
where asked_questions.test_result_id = test_results.id) as test_result_total_max_score

上面写着:[42S22][1054] Unknown column 'test_results.id' in 'where clause'

我尝试过像这样使用变量@test_result_id

select test_results.*,
       @test_result_id := test_results.id,
       ( ... )
where asked_questions.test_result_id = @test_result_id
where asked_questions.test_result_id = @test_result_id) as test_result_total_max_score

它进行评估,但方式错误(可能是因为评估选择值的顺序未定义)。顺便说一句,所有result_in_percents 都对应于第一个结果。

【问题讨论】:

  • 我的猜测是您当前的查询可以用更简单的方式编写。在您的问题中包含示例数据将对此有所帮助。
  • 如前所述,此查询非常粗略。我敢打赌你可以在没有任何子查询的情况下做到这一点,但我们需要一些示例数据来确保它按预期工作。
  • 子查询只能引用直接包含的查询中的表。您试图在嵌套大约 3 层的查询中引用 test_results.id

标签: mysql sql join subquery mysql-5.6


【解决方案1】:

对于那些面临类似问题的人来说,似乎没有简单的解决方案。 首先,您可以尝试像我一样使用连接重写您的子查询(见下文)。但是当你想对分组结果进行分组操作时,你真的是个不开心的人)。一个“肮脏”的解决方案可能是创建函数来克服嵌套子查询的障碍。

create function test_result_in_percents(test_result_id bigint unsigned)
    returns float
begin
    return (
        select sum(tmp.question_right) * 100 / count(*)
        from (select sum(answers.is_chosen = answer_options.is_right) = count(*) as question_right
                   , asked_questions.test_result_id                              as test_result_id
              from answers
                       inner join answer_options on answer_options.id = answers.answer_option_id
                       inner join asked_questions on asked_questions.id = answers.asked_question_id
              where asked_questions.test_result_id = test_result_id
              group by answers.asked_question_id
             ) as tmp
        group by test_result_id
    );
end;

然后,只需使用这个函数:

select (test_result_in_percents(test_results.id)) as `result_percents`
from `test_results`
where `test_results`.`test_id` = 181
  and `test_results`.`test_id` is not null
order by `test_results`.`id` desc;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多