这是我的问题:
UPDATE student_tests,
(SELECT SUM(olc_sta_i_points_earned) AS total, olc_sta_i_stt_num FROM student_answers
JOIN student_tests ON olc_sta_i_stt_num = olc_stt_i_num
) AS a
SET student_tests.olc_stt_i_score = a.total
WHERE a.olc_sta_i_stt_num = student_tests.olc_stt_i_num
没有错误,但它表示零行受影响。
基本上我有两个表:student_tests
和student_answers
测试ID映射到student_answers
表。我想要一个子查询,我可以在其中总结特定测试ID的所有学生答案,然后更新测试表中student_tests
表中的分数列。
我在这里使用where子句做错了吗?还是别的什么?
答案 0 :(得分:1)
您应该明确地将其标记为update
/ join
,而不是join
子句中的where
条件。
您的问题是子查询中没有group by
。似乎没有必要加入student_tests
,所以试试这个:
UPDATE student_tests s JOIN
(SELECT SUM(a.olc_sta_i_points_earned) AS total, a.olc_sta_i_stt_num
FROM student_answers a
GROUP BY a.olc_sta_i_stt_num
) AS a
ON a.olc_sta_i_stt_num = t.olc_stt_i_num
SET s.olc_stt_i_score = a.total