简体   繁体   English

错误:1242 - 子查询返回超过 1 行

[英]error : 1242 - Subquery returns more than 1 row

I am trying to calculate the total credits for each studentID in the enrollment table.我正在尝试计算注册表中每个学生 ID 的总学分。 I have some repeated studentIDs which means that the same student is already enrolled in many sections and each section has different credit.我有一些重复的学生 ID,这意味着同一个学生已经注册了许多部分,并且每个部分都有不同的学分。 When I tried these queries当我尝试这些查询时

    UPDATE
    enrollment
SET
    enrollment.total_balance =(
    SELECT
        SUM(course.Credits * 100)
    FROM
        enrollment
    INNER JOIN section ON enrollment.sectionID = section.ID
    INNER JOIN course ON section.courseID = course.ID
)

It made all studentIDs have the same sum of credits as shown in the picture.它使所有学生 ID 具有相同的学分总和,如图所示。 Same credit value for each studentIDs每个学生 ID 的信用值相同

Then I tried to group studentIDs to separate each credit using "GROUP BY".然后我尝试使用“GROUP BY”对 studentID 进行分组以分隔每个学分。

    UPDATE
    enrollment
SET
    enrollment.total_balance =(
    SELECT
        SUM(course.Credits * 100)
    FROM
        enrollment
    INNER JOIN section ON enrollment.sectionID = section.ID
    INNER JOIN course ON section.courseID = course.ID
    GROUP BY studentID
)

But I got this message error: #1242 - Subquery returns more than 1 row.但我收到此消息错误:#1242 - 子查询返回超过 1 行。

I have tried many things but it didn't work, I am recently using SQL DB and I just want to make the table as shown in this picture.我尝试了很多东西,但没有奏效,我最近在使用 SQL DB,我只想制作如图所示的表。 Final table results决赛桌结果

Thanks in advance.提前致谢。

Please try using your first query with where condition for each student as:请尝试使用您的第一个查询,每个学生的 where 条件为:

UPDATE
enrollment
SET
enrollment.total_balance=(
SELECT
    SUM(course.Credits * 100)
FROM
    enrollment
    INNER JOIN section ON enrollment.sectionID = section.ID
    INNER JOIN course ON section.courseID = course.ID
)
WHERE enrollment.studentID = some id;

If you want to update each studentID with it's own SUM(course.Credits * 100) value, you need to join the table you want to update with the subquery.如果你想用它自己的SUM(course.Credits * 100)值更新每个studentID ,你需要加入你想用子查询更新的表。 But first, you need to update your subquery so that it will return each StudentID with their individual SUM() .但首先,您需要更新您的子查询,以便它将返回每个StudentID及其各自的SUM() Therefore:所以:

SELECT studentID, SUM(course.Credits * 100) AS val
    FROM enrollment
    INNER JOIN section ON enrollment.sectionID = section.ID
    INNER JOIN course ON section.courseID = course.ID
    GROUP BY studentID

Once you have that, you can use it to join enrollment table with matching StudentID references for the update:一旦你的,你可以用它来参加enrollment表匹配StudentID进行更新引用:

UPDATE
    enrollment AS e 
JOIN
(SELECT studentID, SUM(course.Credits * 100) AS val
    FROM enrollment
    INNER JOIN section ON enrollment.sectionID = section.ID
    INNER JOIN course ON section.courseID = course.ID
    GROUP BY studentID) AS tbl2
ON e.studentID=tbl2.studentID
SET 
    e.total_balance = tbl2.val;

Your approach using a subquery is fine.您使用子查询的方法很好。 It just needs to correlated to the outer query:它只需要与外部查询相关联:

UPDATE enrollment e
    SET total_balance = (SELECT SUM(c.Credits * 100)
                         FROM enrollment e2 JOIN
                              section s
                              ON e2.sectionID = s.ID JOIN
                              course c
                              ON s.courseID = c.ID
                         WHERE e2.StudentId = e.StudentId
                        );

Note that table aliases make the query easier to write and to read.请注意,表别名使查询更易于编写和阅读。

Next .下一个 。 . . . . something seems wrong with your data model.您的数据模型似乎有问题。 I don't see why a table called enrollment should contain the total credits for each (repeated) student.我不明白为什么一个名为enrollment的表应该包含每个(重复)学生的学分。 If this information is stored, it should be at the student level.如果存储此信息,则应该是学生级别的。

More importantly, though, you probably shouldn't be storing this data, because you can readily calculate -- and then the data is always accurate.但更重要的是,您可能不应该存储这些数据,因为您可以轻松计算——然后数据总是准确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM