What's wrong with this query:
INSERT INTO Users( weight, desiredWeight ) VALUES ( 160, 145 ) WHERE id = 1;
It works without the WHERE
clause. I've seemed to have forgot my SQL..
What's wrong with this query:
INSERT INTO Users( weight, desiredWeight ) VALUES ( 160, 145 ) WHERE id = 1;
It works without the WHERE
clause. I've seemed to have forgot my SQL..
You simply cannot use WHERE when doing an INSERT statement:
should be:
The WHERE part only works in SELECT statements:
or in UPDATE statements:
The answer is definitively no.
Adding a WHERE clause after INSERT INTO ... VALUES ... is just invalid SQL, and will not parse.
The error returned by MySQL is:
The most important part of the error message is
which shows the specific part the parser did not expect to find here: the WHERE clause.
You can do conditional INSERT based on user input. This query will do insert only if input vars '$userWeight' and '$userDesiredWeight' are not blank
I do not believe the insert has a WHERE clause.
MySQL INSERT Syntax does not support the WHERE clause so your query as it stands will fail. Assuming your
id
column is unique or primary key:If you're trying to insert a new row with ID 1 you should be using:
If you're trying to change the weight/desiredWeight values for an existing row with ID 1 you should be using:
If you want you can also use INSERT .. ON DUPLICATE KEY syntax like so:
OR even like so:
It's also important to note that if your
id
column is an autoincrement column then you might as well omit it from your INSERT all together and let mysql increment it as normal.Insert query doesn't support where keyword*
Conditions apply because you can use where condition for sub-select statements. You can perform complicated inserts using sub-selects.
For example:
By placing a "select" in the insert statement, you can perform multiples inserts quickly.
With this type of insert, you may wish to check for the number of rows being inserted. You can determine the number of rows that will be inserted by running the following SQL statement before performing the insert.
You can make sure that you do not insert duplicate information by using the EXISTS condition.
For example, if you had a table named clients with a primary key of client_id, you could use the following statement:
This statement inserts multiple records with a subselect.
If you wanted to insert a single record, you could use the following statement:
The use of the dual table allows you to enter your values in a select statement, even though the values are not currently stored in a table.
See also How to insert with where clause