Nisarg Shah
Nisarg Shah

Reputation: 53

How to copy a row and insert in same table and also update specific column with user values in mysql?

In MySQL, I am trying to copy a row in the same table and also increment the primary key and further update a column at the same time.

For example:

 1   |   Test1    |  VALUE1 |
 2   |   Test1    |  VALUE2 |

NOTE: I was able to figure out to copy a record and insert it as a new record but I cannot figure out how to update the data at the same time.

UPDATE: I am using something like this:

Insert into table(col1, col2)
select c1, c2
from table
where id = 1

Upvotes: 3

Views: 2257

Answers (1)

forpas
forpas

Reputation: 164099

There is no need for a separate update.
Use the value 'VALUE2' in the select statement for col2:

INSERT INTO tablename(col1, col2) 
SELECT col1, 'VALUE2' 
FROM tablename 
WHERE id = 1

See the demo.

Upvotes: 2

Related Questions