I have two databases, and I need to move data from one to another. The catch is they have a table with different set of columns. Here is how I need to rename columns in migration:

NULL -> ID (should be generated as I add records normally)
NULL -> category_id (should be fixed, e.g. 3)
date_art -> created_at
title_art -> title
short_art -> promo
full_art -> content

Is it possible to accomplish by a single query?

  • Is the ID column an autoincrement column? – Marco Jul 12 '16 at 12:35
  • Yes. It should be an autoincrement column – Viktor Jul 12 '16 at 13:07

The SQL would be like:

insert into tab1 (category_id, created_at, title, promo, content)
select 3, date_art, title_art, short_art, full_art
from   tab2

The ID column will get it's value during the insert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.