I have data in the wp_users , and I want to duplicate data from this table (except for the ID column) into another table called wp_users2 .
If I didnβt like the id column that I want auto increment, I could just do this:
insert into wp_users2 (select *, NULL as ID from wp_users)
So, I know that I can do this by typing all the column headers except the ID, and manually selecting it as NULL,
SELECT NULL as id, col2, col3...
but I would like to do it dynamically. I read this great S.O. post on how to do this, and it works, however I cannot figure out how to take the data that it gives me and put in the insert statement.
INSERT INTO wp_users2 ( SET @sql = CONCAT('SELECT NULL as ID,', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'ID,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'wp_users' AND TABLE_SCHEMA = 'wp1'), ' FROM wp_users'); PREPARE stmt1 FROM @sql; EXECUTE stmt1; )
What is the correct syntax for this?
source share