I have data in a wp_users
table, and I want to duplicate the data from that table (except for the ID column) into another table, called wp_users2
.
If I didn't care about the id
column, which I want to auto-increment, I could just do this:
insert into wp_users2 (select *, NULL as ID from wp_users)
So I know I could do this by typing out all of the column headers except for ID and manually selecting that one as NULL,
SELECT NULL as id, col2, col3...
but I'd like to do it dynamically. I read this great S.O. post about how to do that, and it works, however I can't figure out how to take the data it gives me and put it into an 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's the right syntax for that?
As I understand - id is AUTO_INCREMENT field.
So, try to use this script as an example for your task -