0

I want Copy data from table to same table and ignore id and change value for 1 column (profile_name)

INSERT INTO profiles 
SELECT * FROM profiles where id=1
Mido Bona
  • 13
  • 6

2 Answers2

0
insert into table select col1,col2,col3,'custom value',(select x from y where z=1) from table where id=1

just ignore identity column name and think that you are inserting some values from another table

0
insert into yourtable (id, col1, col2, col3, profile_name)
(select null col1, col2, col3, New_value
  from table )

I understood that you want to copy a existing row , update profile_name column to some new value and id to null

If you have many columns,

SET @insertsql = CONCAT('insert into yourtable SELECT ', 
                       (SELECT REPLACE(REPLACE(GROUP_CONCAT(COLUMN_NAME), 'id,', 'null,') , 'profile_name,', '''newvalue'',')
                          FROM INFORMATION_SCHEMA.COLUMNS 
                         WHERE TABLE_NAME = 'yourtable' 
                           AND TABLE_SCHEMA = 'yourschema'), 
                       ' FROM yourtable');



prepare stmt from @insertsql   
EXECUTE @stmt; 
Valli
  • 1,440
  • 1
  • 8
  • 13