-3

Possible Duplicate:
How to add a where clause in a MySQL Insert statement?

I am writing a SQL INSERT STATEMENT with WHERE clause.

 INSERT INTO House ( ID ,ADDRESS ) VALUES ('12','LONDON') WHERE OWNER='1';

Error i get :

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 OWNER='1'' at line 1
Community
  • 1
  • 1
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140

5 Answers5

4

WHERE is used in UPDATE query. Use UPDATE for your query if got where clause.

sel
  • 4,982
  • 1
  • 16
  • 22
4

You can't use WHERE clause with INSERT Query..

If you want update the record already if the record exist

INSERT INTO House ( OWNER, ID ,ADDRESS ) VALUES ('1', '12','LONDON') 
ON DUPLICATE KEY UPDATE ID = '12', ADDRESS = 'LONDON'

The 'ON DUPLICATE KEY' statement only works on PRIMARY KEY and UNIQUE columns

check this link for more info

mrsrinivas
  • 34,112
  • 13
  • 125
  • 125
1

INSERT INTO doesn't have a WHERE clause. Remove it.

Carlos Vergara
  • 3,592
  • 4
  • 31
  • 56
1

You can onlu use the WHERE clause if you are selecting from a table, or updateing a table.

So you wouls typically have something like

INSERT INTO Table (Columns...) SELECT Columns... FROM SomeOtherTable WHERE Condition

In your case you need to use

INSERT INTO House ( ID ,ADDRESS ) VALUES ('12','LONDON')
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
1

WHERE clause doesnot work with INSERT INTO statement. You can use the clause with a SELECT statement

The correct code for you will be as:

INSERT INTO House(ID,ADDRESS) values(12,'LONDON');

Gapchoos
  • 1,422
  • 5
  • 20
  • 40