Home > OS >  How to insert a row into one table in sql that is exact same as another table but only one column di
How to insert a row into one table in sql that is exact same as another table but only one column di

Time:10-29

I have a table and I want to insert a new row that is exactly the same as one row EXCEPT with one column value different.

col1 col2 col3
joe  ben  sam

My desired output for the new row being added would be

col1 col2 col3
bob  ben  sam
joe  ben  sam

As you can see the original column is still there but we added a new row with the first value being changed from joe to bob.

Any ideas on the most elegant way to do this maybe without hard coding? I was thinking maybe an insert based on a select * where condition but not sure. Any tips would be greatly appreciated. Thank you!

CodePudding user response:

You can do:

insert into t (col1, col2, col3) select 'joe', col2, col3 from t
  • Related