Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL duplicate row, and add some new data

Tags:

mysql

I have found a lot of answers into how to duplicate a row. It is easy enough, however I haven't seem to have found any examples like what I am looking for. I need to duplicate parts of a row, and add some new data as well.

So duplicating a row:

Insert Into TableName (user, course,title) 
Select user, 11 From TableName Where course=6;

How would I duplicate the row, and add my own information to title? Would I put a comma after the select statement?

like image 443
TomC Avatar asked Feb 04 '26 13:02

TomC


1 Answers

You can do it the same way you are modifying information for the course - just provide a hard-coded value:

Insert Into TableName (user, course, title)
Select user, 11, 'My New Title' From TableName Where course=6;
                 ^^^^^^^^^^^^^^

It doesn't matter that the data doesn't come from TableName, so long as a value is provided for the field title that you're inserting into.

like image 98
mellamokb Avatar answered Feb 06 '26 03:02

mellamokb