从子查询的计数中更新值

时间:2012-03-29 19:56:58

标签: mysql

我正在尝试计算用户获得的所有正确答案,并在分数表中更新它。但它给了我一个有趣的小错误。

UPDATE score
SET end_time = CURTIME(), final_score = COUNT(
    SELECT *
    FROM answered
    WHERE (exam_id = 8 and student_login = 'user' and guessed = 1)
)
WHERE exam_id = 8 and student_login = 'user'

错误:

  

#1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   'SELECT * FROM answers WHERE附近(exam_id = 8和student_login =   'user'和gue'在第3行

这应该够简单吧?那么这个错误到底是什么?选择单独工作。并且更新在没有final_score更新的情况下工作。

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

UPDATE score
SET end_time = CURTIME(), final_score = (
    SELECT COUNT(*)
    FROM answered
    WHERE (exam_id = 8 and student_login = 'user' and guessed = 1)
)
WHERE exam_id = 8 and student_login = 'user'

应该这样做。

COUNT不能(据我所知)对子查询进行操作。

答案 1 :(得分:1)

试试这个:

UPDATE score
SET end_time = CURTIME(), final_score = (
    SELECT COUNT(*)
    FROM answered
    WHERE (exam_id = 8 and student_login = 'user' and guessed = 1)
)
WHERE exam_id = 8 and student_login = 'user'

答案 2 :(得分:0)

试试这个..

UPDATE score
SET end_time = CURTIME(), final_score = (
    SELECT cnt=count(*)
    FROM answered
    WHERE (exam_id = 8 and student_login = 'user' and guessed = 1)
)
WHERE exam_id = 8 and student_login = 'user'