【问题标题】:MySQL INSERT INTO failing at WHERE clauseMySQL INSERT INTO 在 WHERE 子句中失败
【发布时间】:2015-04-07 06:00:41
【问题描述】:

好吧,我被这个难住了:

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

这是表格描述(稍微消毒):

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,用于 redhat-linux-gnu (x86_64),使用 readline 5.1(我知道它较旧,但我目前无法升级此机器)。

【问题讨论】:

标签: mysql sql


【解决方案1】:

如果您要更新现有行,可以这样:

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

【讨论】:

    【解决方案2】:

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

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

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

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-05
      • 2012-10-08
      • 2014-03-13
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      相关资源
      最近更新 更多