MySQL简单查询错误

时间:2015-02-15 16:44:11

标签: java mysql insert-update

我是MySQL新手,我在下面写了一个简单的查询:

CREATE Table tblFeedBack
(
    `FeedBackID` INT AUTO_IncremeNT,
    `UserID` INT,
    `Inserted_TS` TIMESTAMP ,
    `FeedBackValue` VARCHAR(100),
     PRIMARY KEY (FeedBackID)
);

CREATE PROCEDURE tblFeedBack_InsertUpdate
(
    IN U_ID INT,
    IN FB_Value VARCHAR(50)
)
BEGIN    
    IF ((Select COUNT(*) From 'tblFeedback') < 3)
        BEGIN
            INSERT INTO 'tblFeedBack' (`UserID`,`FeedBackValue`) 
            VALUES (U_ID,FB_Value);
        END
    ELSE
        BEGIN
            DECLARE @MostRecentFID INT;

            SELECT TOP 1 `FeedBackID` FROM tblFeedback 
            WHERE UID = U_ID
            ORDER BY `Inserted_TS` DESC
            INTO @MostRecentFID;

            UPDATE tblfeedback
            SET `FeedBackValue` = @FeedBackValue
            WHERE `FeedBackID` = @MostRecentFID 
        END
END

我收到此错误:

 Schema Creation Failed: 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 ''tblFeedBack' (`UserID`,`FeedBackValue`) VALUES (1,'12')' at line 9

任何人都可以帮我解决这个问题吗? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

插入查询中的表名称不同。一个包含'tblFeed b ack',其他包含'tblFeed B ack'

另外,请尝试删除查询中表名附近的单引号。

答案 1 :(得分:0)

MySQL不支持TOP您希望在订购时使用LIMIT 1

Select `FeedBackID` FROM tblFeedback 
WHERE UID = U_ID
ORDER BY `Inserted_TS` DESC
LIMIT 1
INTO @MostRecentFID;

此外,当您使用引号查询表时,它将区分大小写。

IF ((Select COUNT(*) From 'tblFeedback') < 3)

应该是

IF ((Select COUNT(*) From 'tblFeedBack') < 3)

修复这两个错误,它应该可以正常工作。