2017-07-02 143 views
1
mysql> DESC transaction; 
+-------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+-------+--------------+------+-----+---------+----------------+ 
| id | int(11)  | NO | PRI | NULL | auto_increment | 
| title | varchar(100) | NO |  | NULL |    | 
| from | int(11)  | NO |  | NULL |    | 
| to | int(11)  | NO |  | NULL |    | 
| cost | int(11)  | NO |  | NULL |    | 
+-------+--------------+------+-----+---------+----------------+ 
5 rows in set (0.01 sec) 

mysql> INSERT INTO transaction (title, from, to, cost) VALUES ('Ham', 1, 4, 9000); 
ERROR 1064 (42000): 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 'from, to, cost) VA 
LUES ('Ham', 1, 4, 9000)' at line 1 

我試圖插入數據到'transaction'表。我認爲查詢很好,但它說錯誤。我的問題是什麼?請幫幫我!錯誤1064(42000):您的SQL語法有錯誤;我找不到我做錯了

+0

我想通出。我想這是因爲在查詢中使用'from'。這樣對嗎? –

回答

0

在MySQL中,某些單詞如SELECT,INSERT,FROM等是保留字。由於它們有特殊的含義,只要您將它們用作表名,列名或其他類型的標識符,MySQL就會將其視爲語法錯誤 - 除非您使用反引號包圍標識符。

有關詳細信息,請使用保留的關鍵字作爲列名看https://dev.mysql.com/doc/refman/5.7/en/keywords.html

1
  • 避免。
  • 如果您需要使用它們,請在引用它們時使用雙引號。編寫select語句時也會出現語法錯誤。不要忘了與雙引號

用途使用它們:中

select "from", cost from... 

代替:

select from, cost from... 
1

CHANGE INSERT TSQL TO:

INSERT INTO `transaction` (`title`, `from`, `to`, `cost`) VALUES ('Ham', 1, 4, 9000); 
相關問題