For duplicating an entry I would like to use the following syntax:
insert into TABLE select * from TABLE where ...
However, as the first column is an auto-increment primary key, this value must be different. My workaround was to specifiy all fields in the select query instead of using the asterisk and then leaving the primary key field blank. Since my table has more than thirty fields that unfortunately keep changing, I am looking for a solution that I can implement in a script and that does not need to be modified when the table structure changes. Any ideas? Thank you very much!
MySQL does not provide a way to eliminate fields like this. However, if you know you’ll always want all the fields but the primary key, then there’s really not much overhead to fetching the primary key as well. It will be much easier to exclude the primary key in your code.
If it’s really important to get this, you could execute
DESCRIBE TABLEto get a list of columns and then construct your query. However, this is likely just to make things a lot more complicated. I would advise just ignoring the primary key.