Home > OS >  Using INSERT with condition
Using INSERT with condition

Time:11-13

there is a table customers with three columns (Id, name, Phone). For example there is charachter with id 1 and name is Tom, and there are a lot of differents id and names so i need to add a phone number solely for tom in this sutiation, how can i do it? i tried this:

INSERT INTO Customers (Phone) values ('a Number') WHERE Id = 1;

I can get that i use condition "where" wrong, how should i use it right in my situation, please help, and thanks.

CodePudding user response:

It seems that you actually want an update here:

UPDATE Customers
SET Phone = 'a Number'
WHERE Id = 1;

If you really do want to add a new record, then drop the WHERE clause:

INSERT INTO Customers (Phone) VALUES ('a Number');

If the Id column be auto increment, then MySQL will auto generate a value for the Id.

CodePudding user response:

In the described case (update of already existing record) you should use UPDATE statement instead of INSERT:

UPDATE Customers SET Phone = "customer_phone_number_here" WHERE Id = 1;

Hope, this link would be useful as well.

  • Related