2011-07-26 94 views
2

我想使用關鍵字進行MySQL查詢。有沒有什麼方法來封裝這些關鍵字,以便我可以使用它們呢?在查詢中使用MySQL關鍵字?

這裏是不會執行查詢:

INSERT INTO PendingPasswordRecovery (key, userID, TimeModified, TimeCreated, TimeDeleted, IsDeleted) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false) 

正如你可能已經猜到了已經,「鑰匙」是,我使用的查詢關鍵詞,這家飯店目前也的名字我想要使​​用的列。

回答

4

使用反引號:

INSERT INTO PendingPasswordRecovery (`key`, `userID`, `TimeModified`, `TimeCreated`, `TimeDeleted`, `IsDeleted`) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false) 

我已經加入反引號到所有的列名,但你可以做它的關鍵字。反引號被稱爲「標識符引號字符」。欲瞭解更多信息,請參見:http://dev.mysql.com/doc/refman/5.1/en/reserved-words.htmlhttp://dev.mysql.com/doc/refman/5.1/en/identifiers.html

2

只需將它們用反引號

INSERT INTO PendingPasswordRecovery (`key`, userID, TimeModified, TimeCreated, TimeDeleted, IsDeleted) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false) 
            ^^^^^^^^^ 

Here's the list of MySQL reserved keywords.

1

使用`逃脫列名:

INSERT INTO PendingPasswordRecovery (`key`, `userID`, `TimeModified`, `TimeCreated`, `TimeDeleted`, `IsDeleted`) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false) 
-1

我不認爲有是這樣做的一種方式。 嘗試key但更好的方法是始終重命名您的字段