2015-04-07 86 views
0

好吧,我難倒就這一個:MySQL的INSERT INTO未能在WHERE子句

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(我知道它比較老,但我現在不能升級這個盒子)。

+1

希望這個答案會有所幫助:http://stackoverflow.com/a/485062/2451726 – Arulkumar

+0

INSERT INTO不支持WHERE 這個http://stackoverflow.com/questions/485039/mysql-insert-where-query將有所幫助。 – starplateena

+1

而不是插入查詢您使用更新查詢在哪裏條件 – Saty

回答

2

如果你正在尋找更新現有行,那就是:

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

您沒有正確使用INSERT聲明,如果您使用VALUES子句,則無法對其應用WHERE條件。

這裏是適當的語法相同的查詢:

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

希望這會幫助你。