mysql错误,如果查询

时间:2014-12-21 20:50:30

标签: mysql

当我尝试运行以下查询时,我收到以下错误。有人帮忙!

 IF EXISTS (SELECT * 
               FROM sch_scores s2 
               WHERE s2.ResultID = "24614" 
                 AND s2.FormatID = "9" 
                 )
    BEGIN
       UPDATE sch_scores 
       SET Score = '4' 
       WHERE ResultID = "24614" 
         AND FormatID = "9" 
        )
    END 
    ELSE BEGIN
       INSERT INTO dbo.sch_scores (school_id, ScoreID, ResultID, FormatID, Score, Status)
       VALUES (1, 30836, 24614, 9, 4, 'Active') 
    END

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT * FROM sch_scores s2 WHERE s2.Result' at line 1

2 个答案:

答案 0 :(得分:0)

控制流结构(如IFWHILE等)只能用于存储过程或函数。您可以将代码放在存储过程中,或者您真正需要的内容可能是INSERT ... ON DUPLICATE KEY UPDATE。阅读更多相关信息here

虽然看起来你需要一个insert...on duplicate key语句,但是manual可以找到许多存储过程的例子。

答案 1 :(得分:0)

您需要像使用WHERE子句一样用引号括起ResultID,FormatID和Score:

VALUES(1,30836,' 24614',' 9',' 4','有效')