错误1064 (42000):SQL语法中有错误;我找不出我做错了什么

时间:2022-04-24 14:27:03
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

I tried to insert data to 'transaction' table. I think the query is fine but it is saying error. What's is my problem? Please, help me!

我试图向“事务”表插入数据。我认为这个查询很好,但是它说的是错误。是我的问题是什么?请帮帮我!

3 个解决方案

#1


1  

  • Avoid using reserved keywords as column names.
  • 避免使用保留的关键字作为列名。
  • Still if you need to use them, use double quotes while referencing them. You'll get a syntax error while writing a select statement as well. Do not forget to use them with double quotes
  • 如果您需要使用它们,请在引用它们时使用双引号。在编写select语句时也会出现语法错误。不要忘记使用双引号

Use:

使用:

select "from", cost from... 

instead of:

而不是:

select from, cost from...

#2


1  

CHANGE INSERT Tsql TO :

将插入Tsql更改为:

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

#3


0  

In MySQL, certain words like SELECT, INSERT, FROM etc. are reserved words. Since they have a special meaning, MySQL treats it as a syntax error whenever you use them as a table name, column name, or another kind of identifier - unless you surround the identifier with backticks.

在MySQL中,某些词如SELECT、INSERT等都是保留词。由于它们有特殊的含义,所以每当您将它们用作表名、列名或其他类型的标识符时,MySQL会将其视为语法错误——除非您将标识符括起来。

For more details, Please see https://dev.mysql.com/doc/refman/5.7/en/keywords.html

有关详细信息,请参见https://dev.mysql.com/doc/refman/5.7/en/keywords.html

#1


1  

  • Avoid using reserved keywords as column names.
  • 避免使用保留的关键字作为列名。
  • Still if you need to use them, use double quotes while referencing them. You'll get a syntax error while writing a select statement as well. Do not forget to use them with double quotes
  • 如果您需要使用它们,请在引用它们时使用双引号。在编写select语句时也会出现语法错误。不要忘记使用双引号

Use:

使用:

select "from", cost from... 

instead of:

而不是:

select from, cost from...

#2


1  

CHANGE INSERT Tsql TO :

将插入Tsql更改为:

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

#3


0  

In MySQL, certain words like SELECT, INSERT, FROM etc. are reserved words. Since they have a special meaning, MySQL treats it as a syntax error whenever you use them as a table name, column name, or another kind of identifier - unless you surround the identifier with backticks.

在MySQL中,某些词如SELECT、INSERT等都是保留词。由于它们有特殊的含义,所以每当您将它们用作表名、列名或其他类型的标识符时,MySQL会将其视为语法错误——除非您将标识符括起来。

For more details, Please see https://dev.mysql.com/doc/refman/5.7/en/keywords.html

有关详细信息,请参见https://dev.mysql.com/doc/refman/5.7/en/keywords.html