MySQL错误子查询返回超过1行

时间:2012-10-23 14:32:35

标签: mysql phpmyadmin

我有以下查询:

SELECT 
    users.*,
    classes.*, 
    evaluation.student_id, 
    evaluation.class_id, 
    evaluation.chapter_title,
    (SELECT
        `score`
     FROM 
        `evaluation`
     WHERE 
        `class_id` = 1 
     AND 
        `id` 
     IN
        (SELECT 
            MAX(`id`)
         FROM
            `evaluation`
         WHERE
            `class_id` = 1
         GROUP BY
            `chapter_title`)
     GROUP BY
        `chapter_title`) 
     AS
        `score`,
     (SELECT
        `total_score`
      FROM
        `evaluation`
      WHERE
        `class_id` = 1
      AND
        `id`
      IN
        (SELECT 
            MAX(`id`)
         FROM
            `evaluation`
         WHERE
            `class_id` = 1
         GROUP BY
            `chapter_title`)
      GROUP BY
         `chapter_title`)
      AS
         `total_score`
      FROM
        (`evaluation`
      INNER JOIN
        `users`
      ON 
        evaluation.student_id=users.id)
      INNER JOIN
        `classes`
      ON
        evaluation.class_id=classes.id
      WHERE
        users.role='student'
      AND
        evaluation.class_id = 1
      AND
        evaluation.student_id = 8

但是当我在phpmyadmin中执行此查询时,它会显示一条错误消息:

#1242 - Subquery returns more than 1 row

查询错误。请帮助。提前谢谢。

我有这张表:

用户 enter image description here

enter image description here

评价 enter image description here

在评估表(最后一张图片)中..我只想返回一个截然不同的chapter_title或一个分组的chapter_title,其id最高且student_id为8。

我需要使用此查询...但返回错误。

4 个答案:

答案 0 :(得分:2)

问题是你的两个查询都返回了多条记录:

SELECT `score`
     FROM `evaluation`
     WHERE `class_id` = 1 
       AND `id` IN (SELECT  MAX(`id`)
                     FROM `evaluation`
                     WHERE `class_id` = 1
                     GROUP BY `chapter_title`)
     GROUP BY `chapter_title`;

SELECT `total_score`
      FROM `evaluation`
      WHERE `class_id` = 1
        AND `id` IN (SELECT  MAX(`id`)
                     FROM `evaluation`
                     WHERE `class_id` = 1
                     GROUP BY `chapter_title`)
      GROUP BY`chapter_title`;

请参阅SQL Fiddle with demo

我将您的查询略微更改为以下内容:

SELECT u.*,
    c.*, 
    e.student_id, 
    e.class_id, 
    e.chapter_title,
    (SELECT `score`
     FROM `evaluation` e1
     WHERE `class_id` = 1 
       AND e.id = e1.id
       AND `id` IN (SELECT  MAX(`id`)
                     FROM `evaluation`
                     WHERE `class_id` = 1
                     GROUP BY `chapter_title`)
     GROUP BY `chapter_title`)  AS`score`,
     (SELECT `total_score`
      FROM `evaluation` e1
      WHERE `class_id` = 1
        AND e.id = e1.id
        AND `id` IN (SELECT  MAX(`id`)
                     FROM `evaluation`
                     WHERE `class_id` = 1
                     GROUP BY `chapter_title`)
      GROUP BY`chapter_title`) AS `total_score`
FROM `evaluation` e
INNER JOIN `users` u
  ON e.student_id=u.id
INNER JOIN `classes` c
  ON e.class_id=c.id
WHERE u.role='student'
  AND e.class_id = 1
  AND e.student_id = 8

请参阅SQL Fiddle with demo

答案 1 :(得分:1)

您需要在所有子查询上使用LIMIT 1,以确保只返回一个值。

<强> E.g。

select users.*, classes.*, evaluation.student_id, evaluation.class_id, evaluation.chapter_title, (
        select `score`
        from `evaluation`
        where `class_id` = 1
            and `id` in (
                select MAX(`id`)
                from `evaluation`
                where `class_id` = 1
                group by `chapter_title`
                )
        group by `chapter_title`
        limit 1
        ) as `score`, (
        select `total_score`
        from `evaluation`
        where `class_id` = 1
            and `id` in (
                select MAX(`id`)
                from `evaluation`
                where `class_id` = 1
                group by `chapter_title`
                )
        group by `chapter_title`
        limit 1
        ) as `total_score`
from (
    `evaluation` inner join `users` on evaluation.student_id = users.id
    )
inner join `classes` on evaluation.class_id = classes.id
where users.role = 'student'
    and evaluation.class_id = 1
    and evaluation.student_id = 8

答案 2 :(得分:1)

以下是corrected fiddle

SELECT
    users.*,
    classes.*,
    evaluation.student_id,
    evaluation.class_id,
    evaluation.chapter_title,
    evaluation.score,
    evaluation.total_score
 FROM
    evaluation
 INNER JOIN users
 ON
    evaluation.student_id=users.id
  INNER JOIN
    classes
  ON
    evaluation.class_id=classes.id
  WHERE
    users.role='student'
  AND
    evaluation.class_id = 1
  AND
    evaluation.student_id = 8
 AND
 EXISTS
    (SELECT
        *
     FROM
        evaluation
     WHERE
        class_id = 1
    HAVING MAX(id) = evaluation.id)
 GROUP BY
    chapter_title

主要变化:

  1. 使用IN代替EXISTS(应该更快,因为它不需要返回所有子行);

  2. 不需要一些子查询。

答案 3 :(得分:0)

您的SELECT scores子查询(第一个)很可能返回多行。它是IN()子句中未使用的唯一一个,其结果作为父查询中的字段返回。由于它“返回”一个字段,它必须导致单个值+单行结果集。