将数据从表复制到同一个表

时间:2017-10-27 23:46:07

标签: php mysql sql

我希望将数据从表复制到同一个表并忽略id并更改1列(profile_name)的值

INSERT INTO profiles 
SELECT * FROM profiles where id=1

2 个答案:

答案 0 :(得分:0)

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

只是忽略标识列名称,并认为您正在从另一个表中插入一些值

答案 1 :(得分:0)

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

我了解您要复制现有行,将profile_name列更新为某个新值,将id更改为null

如果您有多列,

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;