Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL/MariaDB: Copy table to another table with an extra column at the start

In the past, I have successfully copied tables from one to other like this:

INSERT INTO myhistory SELECT p.*, NOW(), '' FROM mymain p

Here, the extra columns were at the end. However, I now have a different case where the extra column is (a) at the beginning of the table and (b) it is an auto-incremented column. So I tried this:

INSERT INTO `starchive` null, SELECT f.* FROM `stflagged` f

This isn’t working. I also tried 0 instead of null, but that fails too. Any ideas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The NULL is part of the select statement so the number of fields match. So:

INSERT INTO `starchive` SELECT NULL, f.* FROM `stflagged` f

Or explicitly list table columns:

INSERT INTO `starchive` (col2, col3, col4) SELECT f.* FROM `stflagged` f

example ref: https://dbfiddle.uk/5vKYKdwC

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading