If you do not want to list all the fields that you must play using information_schema and prepared statements.
This is an example
set @str = (select concat('insert into ', table_name,' (', group_concat(column_name),')',' select ',group_concat(column_name),' from ', table_name, ' where id = 1') from information_schema.columns where table_schema = 'your_db' and table_name = 'your_table' and column_key <> 'PRI'); prepare stmt from @str; execute stmt; deallocate prepare stmt;
You can even convert it to a stored procedure by passing it three parameters (id, table name, and database name) as follows:
delimiter // drop procedure if exists copy_record // create procedure copy_record(in my_id int, in my_db varchar(50), in my_table varchar(50) ) begin set @str = (select concat('insert into ', table_name,' (', group_concat(column_name),')',' select ',group_concat(column_name),' from ', table_name, ' where id = ',my_id) from information_schema.columns where table_schema = my_db and table_name = my_table and column_key <> 'PRI'); prepare stmt from @str; execute stmt; deallocate prepare stmt; end; // delimiter ; call copy_record(1,'db_name','table_name');
source share