首页 文章

MySQL INSERT INTO在WHERE子句中失败

提问于
浏览
0

好吧,我很难过这个:

mysql> INSERT INTO item (col1) VALUES ('testing') WHERE item_name = 'server1';

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 'WHERE item_name = 'server1'' at line 1

这是表desc(稍微消毒):

mysql> desc item;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment | 
| item_name    | varchar(128) | YES  |     |         |                | 
| active       | int(11)      | NO   |     | 0       |                | 
| col1         | varchar(512) | YES  |     | NULL    |                | 
+--------------+--------------+------+-----+---------+----------------+

我已经尝试过INSERT的一些变化,但我无处可去 . 期待有人揭露我所遗忘的任何明显的事情!

运行:mysql Ver 14.12 Distrib 5.0.95,对于使用readline 5.1的redhat-linux-gnu(x86_64)(我知道它已经老了,但我目前无法升级此框) .

2 回答

  • 1

    如果您要更新现有行,则为:

    UPDATE item
      SET col1 = 'testing'
      WHERE item_name = 'server1';
    
  • 2

    您没有正确使用 INSERT 语句,如果使用 VALUES 子句,则无法对其应用 WHERE 条件 .

    以下是具有相应语法的相同查询:

    INSERT INTO item (col1)
    SELECT 'testing'
    FROM yourTable
    WHERE item_name = 'server1';
    

    希望这会帮助你 .

相关问题