-1

Imagine I have some code like this:

$sql = "INSERT INTO tablename (column1, column2, column3) VALUES 
('1','2','3')"; 

How would I convert this into an UPDATE query? I did some searching, but

UPDATE table_name 
SET column1=value1,column2=value2,... 
WHERE some_column=some_value;

doesn't make much sense to me. Thanks

Caspar
  • 169
  • 2
  • 9
  • isn't that sql structure incorrect – M A SIDDIQUI Feb 16 '17 at 07:15
  • why you want to replace this insert query into update ? If I am not wrong, you have condition of inserting and updating records on the basis of condition right ? – Rahul Feb 16 '17 at 07:15
  • Sorry. Fixed it @MASIDDIQUI – Caspar Feb 16 '17 at 07:17
  • I have this code that inserts values into a table when a user creates an account. However, if they want to change their details later, I assume it will require an UPDATE query, right? @rahul_m – Caspar Feb 16 '17 at 07:17
  • UPDATE tablex SET name='xxx', WHERE id=1; go like this... – Soniya Basireddy Feb 16 '17 at 07:19
  • @Caspar It is the way to go, why don't it make sense to you? – Linkan Feb 16 '17 at 07:20
  • @Linkan I'm just confused with the UPDATE syntax, that's all. Looking at these comments, to update the value of column 1, 2 and 3 to the values 1,2 and 3 accordingly I assume I would do this, correct me if I am wrong: $sql = UPDATE tablename SET column1=1,column2=2,column3=3; – Caspar Feb 16 '17 at 07:26
  • @Sona I'm just confused with the UPDATE syntax, that's all. Looking at these comments, to update the value of column 1, 2 and 3 to the values 1,2 and 3 accordingly I assume I would do this, correct me if I am wrong: $sql = UPDATE tablename SET column1=1,column2=2,column3=3; – Caspar Feb 16 '17 at 07:27
  • @Caspar i have given example that is not exactly answer for your question... i will update. – Soniya Basireddy Feb 16 '17 at 07:30
  • UPDATE tablename SET column1='1',column2='2',column3='3' WHERE id=1; go like this – Soniya Basireddy Feb 16 '17 at 07:32
  • This question is NOT at all a duplicate of the stated duplicate. Entirely different question. Granted, the question could have been written in much more clear way. – James John McGuire 'Jahmic' Oct 05 '20 at 17:06

2 Answers2

2

INSERT and UPDATE queries look similar but they do different things.

Inserts => Inserts new nonexisting data into table

Update => Updates existing data inside of the table

That is why the UPDATE query has WHERE clause

So, you can use UPDATE to modify a row which is already inserted.

Sourav Ghosh
  • 1,964
  • 4
  • 33
  • 43
SLIT
  • 33
  • 6
  • I'm just confused with the UPDATE syntax, that's all. Looking at these comments, to update the value of column 1, 2 and 3 to the values 1,2 and 3 according I assume I would do this, correct me if I am wrong: $sql = UPDATE tablename SET column1=1,column2=2,column3=3; – Caspar Feb 16 '17 at 07:25
  • Yes what you are doing is correct. However do not forget about WHERE clause because you can not update nonexisting DATA. – SLIT Feb 16 '17 at 07:30
  • @Caspar `update` needs `where` clause or some other ways(http://stackoverflow.com/questions/30198070/) to update an existing row. But other ways are generally slow by performance – Sourav Ghosh Feb 16 '17 at 07:32
0

I don't know what exactly you are asking, because you are not clear in your question. But according to my thinking you want to know the difference.

Update query edit or update the data in the database. Like this

update table_name
set 'name'=something
where 'id'='some_id'

for more examples and practice go HERE

Insert insert query is just making a new data entry in the database in a table with a unique id, precisely.

HERE is the link for insert example.

tech_geek
  • 1,624
  • 3
  • 21
  • 44